零初始化忽略构造函数
Zero initialization ignoring constructor
以下this and discussion about zero-initialization, I would like to clarify in which situation these two paragraphs zero-initialization CPP reference同时发生:
- As part of value-initialization sequence for non-class types and
for members of value-initialized class types that have no
constructors, including value initialization of elements of aggregates
for which no initializers are provided.
从这一段我了解到class成员必须没有构造函数。
If T is an non-union class type, all base classes and non-static data
members are zero-initialized, and all padding is initialized to zero
bits. The constructors, if any, are ignored.
但是从这个我了解到 constructor of class members, if any (他们不应该有),被忽略。
那么,在什么情况下我可以为 class 成员定义一个构造函数并且仍然 忽略它的构造函数 ?
我对这两个 classes A
和 B
进行了实验,并构建了 A{}
调用 B()
(显然)因为 c=1
。只有 B() = default;
会使 c=0
.
class B
{
public:
B() : c{1} {}
int c;
};
class A
{
public:
A() = default;
B b;
};
可能吗?提前谢谢你。
Then, in which case can I define a constructor for a class member and still ignore its constructor?
静态存储。具有静态存储持续时间的 class 实例将在静态初始化阶段初始化为零,这将忽略构造函数(如果有)。稍后在动态初始化阶段调用构造函数。
Could you give an example?
这是一个 class 具有静态存储持续时间的实例示例:
int main()
{
static A a;
}
以下this and
- As part of value-initialization sequence for non-class types and for members of value-initialized class types that have no constructors, including value initialization of elements of aggregates for which no initializers are provided.
从这一段我了解到class成员必须没有构造函数。
If T is an non-union class type, all base classes and non-static data members are zero-initialized, and all padding is initialized to zero bits. The constructors, if any, are ignored.
但是从这个我了解到 constructor of class members, if any (他们不应该有),被忽略。
那么,在什么情况下我可以为 class 成员定义一个构造函数并且仍然 忽略它的构造函数 ?
我对这两个 classes A
和 B
进行了实验,并构建了 A{}
调用 B()
(显然)因为 c=1
。只有 B() = default;
会使 c=0
.
class B
{
public:
B() : c{1} {}
int c;
};
class A
{
public:
A() = default;
B b;
};
可能吗?提前谢谢你。
Then, in which case can I define a constructor for a class member and still ignore its constructor?
静态存储。具有静态存储持续时间的 class 实例将在静态初始化阶段初始化为零,这将忽略构造函数(如果有)。稍后在动态初始化阶段调用构造函数。
Could you give an example?
这是一个 class 具有静态存储持续时间的实例示例:
int main()
{
static A a;
}