Member const 和Member 有什么区别?
《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 为什么一定要连用。