如何在构造函数中初始化类的成员数组?
问题描述:
我正在尝试执行以下操作:
I am trying to do the following:
class sig
{
public:
int p_list[4];
}
sig :: sig()
{
p_list[4] = {A, B, C, D};
}
我得到一个错误
构造函数中缺少表达式.
missing expression in the constructor.
那我如何初始化一个数组?
So how do I initilalise an array?
答
仅在C ++ 11中:
In C++11 only:
class sig
{
int p_list[4];
sig() : p_list { 1, 2, 3, 4 } { }
};
Pre-11之前,除了在块范围内的自动和静态数组或在名称空间范围内的静态数组之外,无法初始化其他数组.
Pre-11 it was not possible to initialize arrays other than automatic and static ones at block scope or static ones at namespace scope.