| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 1378 人关注过本帖
标题:关于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
快速回复:关于const static数据成员
数据加载中...
 
   



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

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