struct S{ S() : array() {} int array[SIZE];};...S s;// is s.array zero-initialized?
使用gcc进行的快速测试表明情况确实如此,但我想确认我可以依赖这种行为.
(我注意到如果我没有在结构构造函数中显式默认构造数组,则数组元素具有随机值.)
解决方法 是的(突出我的):总结(C++03 8.5)
To value-initialize an object of type T means:
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);
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
if T is an array type,then each element is value-initialized;
otherwise,the object is zero-initialized
…
An object whose initializer is an empty set of parentheses,i.e.,(),shall be value-initialized.
以上是内存溢出为你收集整理的c – 默认构造一个整数数组是否会初始化它?全部内容,希望文章能够帮你解决c – 默认构造一个整数数组是否会初始化它?所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)