标题:关于const static数据成员
取消只看楼主
baobaoisme
Rank: 7Rank: 7Rank: 7
来 自:AVATAR
等 级:黑侠
帖 子:260
专家分:506
注 册:2010-7-9
结帖率:100%
已结贴  问题点数:10 回复次数:2 
关于const static数据成员
“const static数据成员在类的定义体中初始化时,该数据成员仍必须在类的定义体之外进行定义。”看书看到这句注解不解。
const static数据成员的初始化例子:static const int period=30;
我的问题是,为什么在类的定义体中初始化后,还要在类的定义体外进行定义?c++的严格规定吗?为什么我没有在类定义体外进行定义,但是编译的时候却没有错误?求解
搜索更多相关主题的帖子: period 数据 
2011-08-12 15:29
baobaoisme
Rank: 7Rank: 7Rank: 7
来 自:AVATAR
等 级:黑侠
帖 子:260
专家分:506
注 册:2010-7-9
得分:0 
2011-08-12 16:10
baobaoisme
Rank: 7Rank: 7Rank: 7
来 自:AVATAR
等 级:黑侠
帖 子:260
专家分:506
注 册:2010-7-9
得分:0 
确实哦,看了看,很好,原来并不是书上翻译错误,楼上给的那个论坛这个问题的答案还有个外链,讲的超级好,确实是需要在类的外部定义,在类的内部const static 类型即使赋字面值常量也只能算是声明,定义必须在外部进行。
http://publib.boulder.
If a static data member is of const integral or const enumeration type, you may specify a constant initializer in the static data member's declaration. This constant initializer must be an integral constant expression. Note that the constant initializer is not a definition. You still need to define the static member in an enclosing namespace.
非常感谢楼上各位回答。

[ 本帖最后由 baobaoisme 于 2011-8-13 17:17 编辑 ]
2011-08-13 11:30



参与讨论请移步原网站贴子:https://bbs.bccn.net/thread-347432-1-1.html




关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 1.799266 second(s), 8 queries.
Copyright©2004-2025, BCCN.NET, All Rights Reserved