初始化列表中的依赖关系

发布于 2024-11-14 07:49:55 字数 183 浏览 2 评论 0原文

这种行为定义明确吗?

class Foo
{
    int A, B;

    public:

    Foo(int Bar): B(Bar), A(B + 123)
    {
    }
};

int main()
{
    Foo MyFoo(0);
    return 0;
}

Is this behavior well-defined?

class Foo
{
    int A, B;

    public:

    Foo(int Bar): B(Bar), A(B + 123)
    {
    }
};

int main()
{
    Foo MyFoo(0);
    return 0;
}

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

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

发布评论

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

评论(3

情归归情 2024-11-21 07:49:55

不,它是未定义的。 A 将首先被初始化(它位于类定义中的第一个),并且它使用未初始化的 B

类成员按照它们在类定义中出现的顺序进行初始化,而不管它们在初始化列表中的顺序如何。事实上,将成员定义顺序与初始化列表顺序不匹配是不好的做法。

如果您的 Foo 实例碰巧具有静态持续时间,例如 Foo f(0); int main(){},行为是明确定义的。具有静态持续时间的对象在发生任何其他初始化之前进行零初始化;在这种情况下,当构造函数运行时,AB 将为 0。不过,之后的行为是相同的:先是 A,然后是 B,为 A 赋予值 123,为 B 赋予值。 code> 的值是 Bar (还是丑陋的)。

No, it's undefined. A will be initialized first (it's first in the class definition), and it uses uninitialized B.

Class members are initialized in the order they appear in the class definition, irrespective of their order in the initialization list. Indeed, it is bad practice to mismatch the member definition order with the initialization list order.

If your instance of Foo happened to have static duration, like in Foo f(0); int main(){}, the behavior is well-defined. Objects with static duration are zero-initialized before any other initialization takes place; in that case, A and B will be 0 when the constructor is run. After that, though, the behavior is the same: first A then B, giving A a value of 123 and B a value of Bar (still ugly).

十雾 2024-11-21 07:49:55

不,初始化顺序是由类本身的声明顺序定义的。

来自 C++ 标准 12.6.2 [class.base.init] p5

初始化应按以下顺序进行:
— 首先,并且仅对于如下所述的最派生类的构造函数,虚拟基类应按照它们在基类的有向无环图的深度优先从左到右遍历中出现的顺序进行初始化,其中“从左到右”是派生类基类说明符列表中基类名称的出现顺序。
— 然后,直接基类应按照它们出现在基说明符列表中的声明顺序进行初始化(无论 mem 初始化程序的顺序如何)。
——然后​​,非静态数据成员应按照它们在类定义中声明的顺序进行初始化(同样无论 mem 初始化程序的顺序如何)。
— 最后,执行构造函数的主体。
[注意:声明顺序必须确保基类和成员子对象以与初始化相反的顺序销毁。 ]

No, initialization order is defined by the declaration order in the class itself.

From the C++ standard 12.6.2 [class.base.init] p5:

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-11-21 07:49:55

初始化是按照声明中出现的顺序完成的,而不是按照您在构造函数中编写的顺序完成的。

看看这个问题,有点相似:
初始化器列表*参数*评估顺序

Initialization is done in the order of appearance in the declaration, not the order you write it in the constructor.

Look at this question, it's somewhat similar:
Initializer list *argument* evaluation order

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