初始化常量成员数组? [英] initialize constant member array?
问题描述
是否可以在类中初始化一个常量的memeber数组?
我尝试了几种语法,但都失败了。
class A
{
public:
A();
~A();
私人:
const int a [4];
};
A :: A():a = {0,1,2,3}
{
}
Hi,
Is it possible to initialize a constant memeber array in a class?
I tried several syntax but all failed.
class A
{
public:
A();
~A();
private:
const int a[4];
};
A::A():a={0,1,2,3}
{
}
推荐答案
Kyle写道:
是否可以在类中初始化一个常量的memeber数组?
我尝试了几种语法,但都失败了。
A级
{
公开:
A();
~A();
私人:
const int a [4] ;
};
A :: A():a = {0,1,2,3}
{
}
Hi,
Is it possible to initialize a constant memeber array in a class?
I tried several syntax but all failed.
class A
{
public:
A();
~A();
private:
const int a[4];
};
A::A():a={0,1,2,3}
{
}
>
您只能在函数体IIRC中初始化它。例如:使用for
循环,从该常量数组复制,或使用memcpy - 如果数组类型
允许它。
-
Attila aka WW
You can only initialize it inside the function body, IIRC. Eg: with a for
loop, copying from that constant array, or with memcpy - if the arrays type
allows it.
--
Attila aka WW
" Kyle" < ZH ********** @ hotmail.com>在消息中写道
news:40 ******** @ dnews.tpgi.com.au ...
"Kyle" <zh**********@hotmail.com> wrote in message
news:40********@dnews.tpgi.com.au...
是否有可能在类中初始化一个常量的memeber数组?
Hi,
Is it possible to initialize a constant memeber array in a class?
不,不是非静态数组。你为什么这么想?
-Mike
No, not a nonstatic one. Why do you want to?
-Mike
Kyle写道:
Kyle wrote:
是否可以在类中初始化一个常量成员数组?
我尝试了几种语法,但都失败了。
Is it possible to initialize a constant member array in a class?
I tried several syntax but all failed.
struct Array {
int a [4];
};
A级{
私人:
//表示
const数组数组;
public:
A(const Array& a):array(a){}
~A(无效){};
};
int main(int argc,char * argv []){
数组数组= {{0,1,2,3}};
a a(数组);
返回0;
}
struct Array {
int a[4];
};
class A {
private:
// representation
const Array array;
public:
A(const Array& a): array(a) { }
~A(void) { };
};
int main(int argc, char* argv[]) {
Array array = {{0, 1, 2, 3}};
A a(array);
return 0;
}
这篇关于初始化常量成员数组?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!