将默认,构建一个整数数组零初始化它? [英] Will default-constructing an integer array zero-initialize it?
问题描述
如果我有一个结构数组成员,我显式调用数组的默认构造函数在结构的构造,将元素得到默认构造? (在整数阵列的情况下,这将意味着得到零初始化)。
If I have a structure with an array member, and I explicitly call the default constructor of the array in the structure's constructor, will the elements get default-constructed? (In the case of an integer array, this would mean getting zero-initialized).
struct S
{
S() : array() {}
int array[SIZE];
};
...
S s;
// is s.array zero-initialized?
一个快速测试与海湾合作委员会认为,这样的话,但我想确认我可以依靠这种行为。
A quick test with gcc suggests that this is the case, but I wanted to confirm that I can rely on this behaviour.
(我注意到,如果我没有明确的结构构造函数缺省方式构造数组,数组元素具有随机值)。
(I have noticed that if I don't explicitly default-construct the array in the structure constructor, the array elements have random values.)
推荐答案
是(突出矿井):
(C ++ 03 8.5)
(C++03 8.5)
要值初始化类型T的对象是指:
-
如果T是一个类类型(第9条)与用户声明的构造(12.1),则T的默认构造函数(并形成不良的初始化,如果T没有访问默认的构造函数)
if T is a class type (clause 9) with a user-declared constructor (12.1), then the default constructor for T is called (and the initialization is ill-formed if T has no accessible default constructor);
如果T是无用户声明的构造一个非工会类类型,那么每一个非静态>数据成员和T的基类成分是值初始化
if T is a non-union class type without a user-declared constructor, then every non-static > data member and baseclass component of T is value-initialized
如果T是数组类型,那么每一个元素都是值初始化;
,否则,该对象被初始化为零
...
的初始化为空括号的一个目标,即(),应值初始化。
这篇关于将默认,构建一个整数数组零初始化它?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!