关于const static数据成员的问题

whuyotc 2008-09-27 10:46:54
在c++ primer 中文四版p401有这么一句话

const static数据成员在类的定义体中初始化时,该数据成员仍必须在类的定义体之外进行定义
在类内部提供初始化时,成员的定义不必再指定初始化值

可是,我再vs2008下只在类中const static int a=1;
我在类外并没有按照书上所说的 const int a;
但是我这样做还是可以成功编译,并且执行

这是什么原因呢??
...全文
396 24 打赏 收藏 转发到动态 举报
写回复
用AI写文章
24 条回复
切换为时间正序
请发表友善的回复…
发表回复
taodm 2008-09-28
  • 打赏
  • 举报
回复
C++2003标准原文:
9.4.2:
4 If a static data member is of const integral or const enumeration type, its declaration in the class
definition can specify a constant-initializer which shall be an integral constant expression (5.19). In that
case, the member can appear in integral constant expressions. The member shall still be defined in a namespace
scope if it is used in the program and the namespace scope definition shall not contain an initializer.
whuyotc 2008-09-28
  • 打赏
  • 举报
回复
感谢
果然如你所说的
学习了
csdn真乃卧虎藏龙之地也

[Quote=引用 19 楼 ysunwpi 的回复:]
Found below from "http://www.devx.com/tips/Tip/5602"
The C++ Standard now allows initialization of const static data members of an integral type inside their class.

#include <string>
class Buff
{
private:
static const int MAX = 512; //definition
static const char flag = 'a'; //also a defintion
static const std::string msg; //non-integral type; must be defined outside the …
[/Quote]
Vegertar 2008-09-28
  • 打赏
  • 举报
回复
mark
ysunwpi 2008-09-28
  • 打赏
  • 举报
回复
所以C++ primer 4.0看来还挺过时的
ysunwpi 2008-09-28
  • 打赏
  • 举报
回复
Found below from "http://www.devx.com/tips/Tip/5602"
The C++ Standard now allows initialization of const static data members of an integral type inside their class.

#include <string>
class Buff
{
private:
static const int MAX = 512; //definition
static const char flag = 'a'; //also a defintion
static const std::string msg; //non-integral type; must be defined outside the class body
public:
//..
};
const std::string Buff::msg = "hello";

The initialization inside the class body also defines the data member, so it shouldn't be defined outside the class, as opposed const static data members of non-integral types, which have to be defined outside the class body.
knightMickle 2008-09-28
  • 打赏
  • 举报
回复
17楼说的对
lyghe 2008-09-27
  • 打赏
  • 举报
回复
看到代码才知道一楼是正确的。
仔细看看一楼。

解释一下:
独立的看:static定义的成员是归属到类的。const定义的成员是归属到对象的,但是必须在构造函数中初始化。
但是:static const两个关键字一起定义的成员,如一楼所说:只要是整数类型(如 int,char,bool),就在使用的感觉上和全局的常量差不多。我猜测,在每次使用到的时候都会被自动替换成它的值。

关于对 static const int a=10 能不能取地址的问题,我在cb2009上试了是可以的,不必另外存在实现体。不知道其他编译器如何。
taodm 2008-09-27
  • 打赏
  • 举报
回复
取一下那个成员的地址试试就知道了。
OenAuth.Core 2008-09-27
  • 打赏
  • 举报
回复
我还是习惯在类中声明静态变量,在类外赋值,不管是不是CONST,
用习惯了VC6.0,在6.0中是不允许上面的操作的,没试过VS2008
帮顶吧。
kkndciapp 2008-09-27
  • 打赏
  • 举报
回复
up
iory_526 2008-09-27
  • 打赏
  • 举报
回复
传说中的常量折叠?
lsmdiao0812 2008-09-27
  • 打赏
  • 举报
回复
class A
{
public:
static const int i=10;
};

//const int A::i;//可加可不加

int main(array<System::String ^> ^args)
{
cout<<A::i;

getchar();
return 0;
}

试了下确实这样,期待高人
whuyotc 2008-09-27
  • 打赏
  • 举报
回复
我在main函数体中使用a

还是可以正常使用啊



[Quote=引用 2 楼 lyghe 的回复:]
楼上的一件不敢苟同。

编译肯定是没问题的,因为语法没错嘛。要有问题也只会发生在连接时,可能会报告xxx::a找不到实现体。

那么,为什么在VS2008中没报错呢?

是因为你没有任何一个地方使用了这个a,也就不会有个家伙去寻找这个a的实现体,当然就不会有错了。

VS.net的C++代码优化是灰常不错滴,你的这个可怜的a既然没被使用,当然也就被无情的优化掉了。

搂主可以在main函数中使用一下a,应该就会有错误了…
[/Quote]
JJZHK 2008-09-27
  • 打赏
  • 举报
回复
同意lyghe.
lyghe 2008-09-27
  • 打赏
  • 举报
回复
楼上的一件不敢苟同。

编译肯定是没问题的,因为语法没错嘛。要有问题也只会发生在连接时,可能会报告xxx::a找不到实现体。

那么,为什么在VS2008中没报错呢?

是因为你没有任何一个地方使用了这个a,也就不会有个家伙去寻找这个a的实现体,当然就不会有错了。

VS.net的C++代码优化是灰常不错滴,你的这个可怜的a既然没被使用,当然也就被无情的优化掉了。

搂主可以在main函数中使用一下a,应该就会有错误了。
bluecll 2008-09-27
  • 打赏
  • 举报
回复
C++ Primer和你和vs2008编译器都是对的。

你所定义的 const static int a = 1; 是一个常量声明式而非定义式。

通常C++要求你对你所使用的任何东西提供一个定义式,但如果它是个class专属常量又是static且为整数类型(如 int,char,bool),则需特殊处理。

只要不取它们的地址,你可以声明并使用它们而无须提供定义式。 --->重点

但如果你要取常量的地址,或纵使你不取其地址而你的编译器却(不正确的)坚持要看到一个定义式,你就必须另外提供定义式如下:

const int ClassName::a; // 放到实现文件里。由于声明时已设初值,此处不可再设初值。
richbirdandy 2008-09-27
  • 打赏
  • 举报
回复
静态整形常量可以在类中初始化 这个是例外
yndfcd 2008-09-27
  • 打赏
  • 举报
回复
C++ Primier的说法不完全正确.

对于intergral类型来说,可以直接在类定义里声明并定义.非intergral类型,不能在类定义里初始化.
lyghe 2008-09-27
  • 打赏
  • 举报
回复
[Quote=引用 14 楼 taodm 的回复:]
那个能取地址的2位,在2个cpp文件里都取一下看看地址是否相同。
[/Quote]
地址不相同有什么关系?只要你不用地址来比较。
dic_walter 2008-09-27
  • 打赏
  • 举报
回复
VS2008没用过
但VS6.0中,楼主这种用法编译通不过的
加载更多回复(4)

65,206

社区成员

发帖
与我相关
我的任务
社区描述
C++ 语言相关问题讨论,技术干货分享,前沿动态等
c++ 技术论坛(原bbs)
社区管理员
  • C++ 语言社区
  • encoderlee
  • paschen
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
  1. 请不要发布与C++技术无关的贴子
  2. 请不要发布与技术无关的招聘、广告的帖子
  3. 请尽可能的描述清楚你的问题,如果涉及到代码请尽可能的格式化一下

试试用AI创作助手写篇文章吧