Member const 和Member 有什么区别?

zhuoyelee zhuoyelee 墨衍会员 · AI 创作全网分发 墨衍智能分发 通过墨衍一键同步至各平台 可同步平台 CSDN微信公众号微博知乎掘金百家号博客园抖音小红书 你的文章也可以这样分发 墨衍会员 ¥399起/年,写一次发全网 我也要 + 加入社区
《c++ programming lanauage》是这样写的:

10.4.6.2Member Constants

It is also possible to initialize a static integral constant member by adding a constant-expression initializer to its member declaration. For example:

class :Curious {
static const int c1 =7; //ok ,but remember definition
static int c2 =11; // error : not const
const int c3 =13; // error: not static
static const int c4 = f(17); // error: in-class initializer not constant
static const float c5 =7.0; // error: in-class not integral
//....
};

If(and only if) you use an initalized member in a way that requires it to be stored as an object in memory, the member must be (uniquely) defined somewhere. The initializer may not be repeated:
const int Curious::c1; // neccessory, but don't repeat initializer here
const int* p= &Curious::c1; //ok Curious::c1 has been defined

那位高手能帮我解释一下上面的意思嘛?谢谢。
其中,static 和const 为什么一定要连用。
...全文
评论 5
收藏 已收藏
分享
返回顶部
zhuoyelee
C语言 70,040 成员 243,237 内容