非静态数据成员按什么顺序初始化?

发布于 2024-08-29 14:26:28 字数 214 浏览 7 评论 0原文

在下面的代码中,当X的ctor被调用时,AB的ctor会先被调用吗?它们在类主体中的放置顺序是否控制这一点?如果有人可以提供一段来自 C++ 标准的文本片段来讨论这个问题,那就完美了。

class A {};
class B {};
class X
{
 A a;
 B b;
};

In the following code, when the ctor of X is called will the ctor of A or B be called first? Does the order in which they are placed in the body of the class control this? If somebody can provide a snippet of text from the C++ standard that talks about this issue, that would be perfect.

class A {};
class B {};
class X
{
 A a;
 B b;
};

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

吲‖鸣 2024-09-05 14:26:28

顺序是它们在类定义中出现的顺序 - 这来自 C++ 标准的第 12.6.2 节:

5 初始化应在
以下顺序:

——首先,并且仅用于
最派生的构造函数
如下所述的类,虚拟基
类应在初始化
它们出现的顺序是深度优先的
从左到右遍历
基数的有向无环图
类,其中“从左到右”是
基类的出现顺序
派生类中的名称
基本说明符列表。

——然后​​,直接
基类应在中初始化
声明顺序如出现在
基本说明符列表(无论
内存初始化器的顺序)。

——那么,非静态数据成员应该是
按照它们的顺序初始化
在类定义中声明
(同样无论顺序如何
内存初始化器)。

——最后是正文
的构造函数被执行。 [笔记:
申报令的授权是
确保基类和成员子对象
以相反的顺序被销毁
初始化。 ]

The order is the order they appear in the class definition - this is from section 12.6.2 of the C++ Standard:

5 Initialization shall proceed in the
following order:

— First, and only for
the constructor of the most derived
class as described below, virtual base
classes shall be initialized in the
order they appear on a depth-first
left-to-right traversal of the
directed acyclic graph of base
classes, where “left-to-right” is the
order of appearance of the base class
names in the derived class
base-specifier-list.

— Then, direct
base classes shall be initialized in
declaration order as they appear in
the base-specifier-list (regardless of
the order of the mem-initializers).

— Then, nonstatic data members shall be
initialized in the order they were
declared in the class definition
(again regardless of the order of the
mem-initializers).

— Finally, the body
of the constructor is executed. [Note:
the declaration order is mandated to
ensure that base and member subobjects
are destroyed in the reverse order of
initialization. ]

も星光 2024-09-05 14:26:28

初始化始终按照类成员在类定义中出现的顺序进行,因此在示例中先是 a,然后是 b

每个成员的初始化之间有一个序列点,您可以将对尚未初始化的成员的引用传递到类成员的构造函数中,但您只能在有限的方式(例如将其地址形成指针),其他用途很可能会导致未定义的行为。

类成员的销毁总是以与构造相反的顺序发生。

基类和成员的初始化顺序在 12.6.2 [class.base.init]/5 中定义。

Initialization is always in the order that the class members appear in your class definition, so in your example a, then b.

There is a sequence point between the initialization of each member and you can pass a reference to a yet-to-be initialized member into the constructor of a class member but you would only be able to use it in limited ways (such as taking its address to form a pointer), other uses may well cause undefined behaviour.

Destruction of class members always happens in the reverse order of construction.

Order of initialization of bases and members is defined in 12.6.2 [class.base.init]/5.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文