静态数组初始化 [英] static array initialization
问题描述
你好,
我真的不明白最简单的事情。我有一个静态的
指向类的指针数组。我们如何初始化它?
class classA
{
public:
int x1;
};
班级B
{
公开:
void foo(){cout<< m_ptr [0] - > X 1; }
静态classA * m_ptr [4];
};
classA * classB :: m_ptr [0] = 0 ; //这不正确!!!
classA * classB :: m_ptr [1] = 0; //这不正确!!!
classA * classB :: m_ptr [2] = 0; //这不正确!!!
classA * classB :: m_ptr [3] = 0; //这不正确!!!
int main()
{
classB * ptr = new classB;
ptr-> foo();
返回0;
}
>
有什么想法吗?如果是m_ptr不是一个指针数组我会初始化它
像这样:classA * classB :: m_ptr = 0;"
现在阵列怎么样?
Hello,
I really don''t understand the simplest thing at the moment. I have a static
array of pointers to a class . How do we initialize it?
class classA
{
public:
int x1;
};
class classB
{
public:
void foo() { cout<< m_ptr[0]->x1; }
static classA* m_ptr[4];
};
classA* classB::m_ptr[0]=0; // This is not correct !!!
classA* classB::m_ptr[1]=0; // This is not correct !!!
classA* classB::m_ptr[2]=0; // This is not correct !!!
classA* classB::m_ptr[3]=0; // This is not correct !!!
int main()
{
classB* ptr = new classB;
ptr->foo();
return 0;
}
Any idea? If "m_ptr" was not an array of pointers I would had initialize it
like this: "classA* classB::m_ptr=0;"
What about arrays now?
推荐答案
" Makis Papapanagiotou" <硒******************** @ siemens.com>在消息中写道
news:bj ********** @ news.mch.sbs.de ...
"Makis Papapanagiotou" <Se********************@siemens.com> wrote in message
news:bj**********@news.mch.sbs.de...
你好,
<我真的不明白最简单的事情。我有一个
指向类的静态指针数组。我们如何初始化它?
班级A
{
公共:
int x1;
};
班级classB
{
公开:
void foo(){cout<< m_ptr [0] - > X 1; }
static classA * m_ptr [4];
};
classA * classB :: m_ptr [0] = 0; //这不正确!!!
classA * classB :: m_ptr [1] = 0; //这不正确!!!
classA * classB :: m_ptr [2] = 0; //这不正确!!!
classA * classB :: m_ptr [3] = 0; //这不正确!!!
int main()
{classB * ptr = new classB;
ptr-> foo ();
返回0;
}
任何想法?如果是m_ptr这不是一个指针数组我会像这样初始化
:classA * classB :: m_ptr = 0;"
现在阵列怎么样?
Hello,
I really don''t understand the simplest thing at the moment. I have a static array of pointers to a class . How do we initialize it?
class classA
{
public:
int x1;
};
class classB
{
public:
void foo() { cout<< m_ptr[0]->x1; }
static classA* m_ptr[4];
};
classA* classB::m_ptr[0]=0; // This is not correct !!!
classA* classB::m_ptr[1]=0; // This is not correct !!!
classA* classB::m_ptr[2]=0; // This is not correct !!!
classA* classB::m_ptr[3]=0; // This is not correct !!!
int main()
{
classB* ptr = new classB;
ptr->foo();
return 0;
}
Any idea? If "m_ptr" was not an array of pointers I would had initialize it like this: "classA* classB::m_ptr=0;"
What about arrays now?
初始化任何数组的方式相同
classA * classB :: m_ptr [4] = {0,0,0,0};
但由于零初始化是默认值,因此您根本不需要初始化
。
classA * classB: :m_ptr [4];
john
Same way you initialise any array
classA* classB::m_ptr[4] = { 0, 0, 0, 0 };
but since zero initialisation is the default, you don''t need to initialise
it at all.
classA* classB::m_ptr[4];
john
" Makis Papapanagiotou" <硒******************** @ siemens.com>在消息中写道
news:bj ********** @ news.mch.sbs.de ...
"Makis Papapanagiotou" <Se********************@siemens.com> wrote in message
news:bj**********@news.mch.sbs.de...
你好,
<我真的不明白最简单的事情。我有一个
指向类的静态指针数组。我们如何初始化它?
班级A
{
公共:
int x1;
};
班级classB
{
公开:
void foo(){cout<< m_ptr [0] - > X 1; }
static classA * m_ptr [4];
};
classA * classB :: m_ptr [0] = 0; //这不正确!!!
classA * classB :: m_ptr [1] = 0; //这不正确!!!
classA * classB :: m_ptr [2] = 0; //这不正确!!!
classA * classB :: m_ptr [3] = 0; //这不正确!!!
Hello,
I really don''t understand the simplest thing at the moment. I have a static array of pointers to a class . How do we initialize it?
class classA
{
public:
int x1;
};
class classB
{
public:
void foo() { cout<< m_ptr[0]->x1; }
static classA* m_ptr[4];
};
classA* classB::m_ptr[0]=0; // This is not correct !!!
classA* classB::m_ptr[1]=0; // This is not correct !!!
classA* classB::m_ptr[2]=0; // This is not correct !!!
classA* classB::m_ptr[3]=0; // This is not correct !!!
试试这个 -
classA * classB :: m_ptr [] = {0};
Try this -
classA* classB::m_ptr[]={0};
这篇关于静态数组初始化的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!