请问c++标准中,到底是否可以直接在 class中 初始化static member data?中讲得不一致啊...有看过cp的老大讲解下嘛!
seok 2004-11-26 09:28:44 请问c++标准中,到底是否可以直接在 class中 初始化static member data?<c++primer>中讲得不一致啊...有看过cp的老大讲解下嘛!
<c++ primer中文版>(cp)p527中说(我懒得打字,直接复制英文版内容)
In general, a static data member is initialized outside the class definition.
.......
As a special case, a const static data member of integral type can be initialized within the class body with a constant value.
.......
Because name is an array (and is not of integral type) it cannot be initialized within the class body. Any attempt to do so results in a compile-time error. For example:
class Account {
// ...
private:
static const int nameSize = 16; // ok: integral type
static const char name[nameSize] ="Savings Account"; // error
};
意思就是说在 class中
只能直接初始化static const int类型的值
---------------------------------------------
但是在cp546,却有这样的写法,而且是正确的:
class Screen {
public:
// bkground refers to the static member
// declared later in the class definition
Screen& clear( char = bkground );
private:
static const char bkground = '#';
};
这里直接在 class中 初始化了static const char
与前面矛盾
我用gcc调试提示:ISO C++ forbids in-class initialization of non-const
也就是说,只要是限定为static const,都可以在 class中 直接初始化
而不是必须要限定为static const int
彻底弄混了...搞不懂...
有看过cp的老大讲解下嘛!