请问类的常量数据成员可以是数组吗?

  • 主题发起人 主题发起人 guest
  • 开始时间 开始时间

guest

Moderator
管理成员
注册
2002-10-07
消息
402,176
荣誉分数
76
声望点数
0
<HTML>class A
{
public:
A();
private:
const char a[12];
...
}
如果是,怎么初始化啊?列表初始化不支持
A::A():a("China");
怎么办?
多谢啦。</HTML>
 
<HTML>const char a[12] = {'C', 'h', 'i', 'n', 'a', '
 
<HTML>const char a[] = "china"</HTML>
 
我是说常数据成员,不是问一般的初始化。

<HTML>你知道,常量数据成员必须初始化的,而且只能通过构造函数初始化列表初始化?
不可以对一个常成员采用赋值的方式,而且类成员不可以有初值。</HTML>
 
RE: 我是说常数据成员,不是问一般的初始化。

<HTML>maybe use constructor? I learnd C++ some time ago, but I remember we probably can do it that way. like
A:a("China") {}
Anyway, there is lots of people here to earn money using C++ and you can get the anwser very quickly.
If I'm wrong, don't call me stupid.</HTML>
 
RE: 我是说常数据成员,不是问一般的初始化。

<HTML>since it's a static variable, you couldn't initialize it later including constructor, which must be initialized as being defined. I think, it's no different from standard C. "busy_bf"'s answer may be right for C++ but not C.
By the way, why don't you test it by yourself? It's simple and not ambigulous in fact. Try it now.</HTML>
 
RE: 我是说常数据成员,不是问一般的初始化。

<HTML>Sorry, please ignore my last opinion, the "static" word isn't mentioned originally at all. So, my answer makes no sense. Sorry for misleading you.</HTML>
 
RE: 我是说常数据成员,不是问一般的初始化。

<HTML>class A
{
public:
A();
~A();
private:
const char a[12];

} ;

//A::A():a('C','h','i','n','a','
 
above example was compiled by Gcc, not in VC++

<HTML></HTML>
 
我用了很多种方法,都不行,以上答案也不行

<HTML></HTML>
 
后退
顶部