请问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的老大讲解下嘛!
...全文
530 8 打赏 收藏 转发到动态 举报
写回复
用AI写文章
8 条回复
切换为时间正序
请发表友善的回复…
发表回复
seok 2004-11-27
  • 打赏
  • 举报
回复
对阿
double不是integral type的嘛!

但是在gcc中仍然可以正常编译 class中 的
static const double d=1.0;
而且没有error和warning!!!!!!

搞不懂噢!!!!!!!!!!
谁知道为什么?
choujs 2004-11-27
  • 打赏
  • 举报
回复
integral != integer

我理解 integral type 可能是指基本数据类型。
whoho 2004-11-27
  • 打赏
  • 举报
回复
integral类型不只是int
包括int, long ,char以及这三个加上(如果可以)signed, signed, short等修饰形成的类型等
seok 2004-11-26
  • 打赏
  • 举报
回复
As a special case, a const static data member of integral type can be initialized within the class body with a constant value. For example, if we decided to use an array of characters instead of a string to store the name of the account, we could have specified the size of the array using a const data member of type int. For example:

// header file

class Account {
// ...
private:
static const int nameSize = 16;
static const char name[nameSize];
};

// text file

const int Account::nameSize; // member definition needed
const char Account::name[nameSize] = "Savings Account";


--------------------------
这个是cp527的原文

而且强调在 class外 的
const int Account::nameSize; // member definition needed
是必须有的

但是gcc中,在 class外 可以不要这句
难道说是编译器对c++标准的支持问题??

这样也就可以说明为什么double不是integral type
在gcc中仍然可以正常编译 class中 的
static const double d=1.0;
seok 2004-11-26
  • 打赏
  • 举报
回复
那c++ primer中的这句
As a special case, a const static data member of integral type can be initialized within the class body with a constant value.

故意强调integral type是什么意思哦?
Squall1009 2004-11-26
  • 打赏
  • 举报
回复
const static的数据是在声明类的时候直接初始化的.
非const的static的数据必须在类外面
seok 2004-11-26
  • 打赏
  • 举报
回复
晕死...
是integral type哦...

问题在于,我在 class中 用
static const double d=1.0:

gcc也编译正确啊!
连个warning都没有
double不是integral type的嘛!
earl808 2004-11-26
  • 打赏
  • 举报
回复
没有说矛盾啊
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
};

69,371

社区成员

发帖
与我相关
我的任务
社区描述
C语言相关问题讨论
社区管理员
  • C语言
  • 花神庙码农
  • 架构师李肯
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧