如何在构造函数中初始化类的成员数组? [英] How to initialise a member array of class in the constructor?
本文介绍了如何在构造函数中初始化类的成员数组?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在尝试执行以下操作:
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.
这篇关于如何在构造函数中初始化类的成员数组?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文