这个问题在这里已有答案:
默认构造函数,POD的初始化和C ++ 11 2中的 隐式类型转换关于Bjarne Stroustrup的The C ++ Programming Language,4th Edition,17.6.3.1
内置成员的“默认初始化”使该成员未初始化。
引用默认编译器生成的构造函数。
但是,在17.6.2中我们有以下代码
struct S { string a; int b; }; S f(S arg) { S s0 {}; // default construction: {"",0} .. }其中b默认初始化为0。
那么,我在这里错过了什么?
This question already has an answer here:
Default constructors, initialization of POD and implicit type conversions in C++11 2 answersOn Bjarne Stroustrup's The C++ Programming Language, 4th Edition, 17.6.3.1 it is stated that
The ‘‘default initialization’’ of a built-in member leaves that member uninitialized.
referring to the default compiler generated constructor.
However, in 17.6.2 we have the following code
struct S { string a; int b; }; S f(S arg) { S s0 {}; // default construction: {"",0} .. }where b is default initialized to 0.
So, what am I missing here ?
最满意答案
您正在进行“ 聚合初始化 ”,而不是默认初始化。 在聚合初始化中,未指定的成员经历值初始化(例如,对于整数为零)。
You are doing "aggregate initialization," not default initialization. And within aggregate initialization, unspecified members undergo value initialization (e.g. zero for integers).
更多推荐
发布评论