C++ 成员构造函数和析构函数调用的顺序

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2254263/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-27 22:44:01  来源:igfitidea点击:

Order of member constructor and destructor calls

c++

提问by sbk

Oh C++ gurus, I seek thy wisdom. Speak standardese to me and tell my if C++ guarantees that the following program:

哦,C++ 大师,我寻求你的智慧。对我说标准语并告诉我 C++ 是否保证以下程序:

#include <iostream>
using namespace std;

struct A
{
    A() { cout << "A::A" << endl; }
    ~A() { cout << "A::~" << endl; }
};

struct B
{
    B() { cout << "B::B" << endl; }
    ~B() { cout << "B::~" << endl; }
};

struct C
{
    C() { cout << "C::C" << endl; }
    ~C() { cout << "C::~" << endl; }
};

struct Aggregate
{
    A a;
    B b;
    C c;
};

int main()
{
    Aggregate a;
    return 0;
}

will always produce

总会产生

A::A
B::B
C::C
C::~
B::~
A::~

In other words, are members guaranteed to be initialized by order of declaration and destroyed in reverse order?

换句话说,是否保证成员按声明顺序初始化并以相反顺序销毁?

回答by dirkgently

In other words, are members guaranteed to be initialized by order of declaration and destroyed in reverse order?

换句话说,是否保证成员按声明顺序初始化并以相反顺序销毁?

Yes to both. See 12.6.2

是的。见 12.6.2

6Initialization 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, non-static 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 compound-statement of the constructor body is executed. [ Note: the declaration order is mandated to ensure that base and member subobjects are destroyed in the reverse order of initialization. —end note ]

6初始化应按以下顺序进行:

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

  • 然后,直接基类应按照它们出现在基说明符列表中的声明顺序进行初始化(不管内存初始化器的顺序如何)。

  • 然后,非静态数据成员应按照它们在类定义中声明的顺序进行初始化(同样不管 mem 初始化程序的顺序)。

  • 最后,执行构造函数体的复合语句。[注意:声明顺序是为了确保基类和成员子对象按照初始化的相反顺序销毁。——尾注]

回答by AnT

Yes, they are (non-static members that is). See 12.6.2/5 for initialization (construction) and 12.4/6 for destruction.

是的,它们是(即非静态成员)。初始化(构造)见 12.6.2/5,销毁见 12.4/6。

回答by wilhelmtell

Yes, the standard guarantees objects get destructed in the reverse order they were created. The reason is that one object may use another, thus depend on it. Consider:

是的,标准保证对象以它们创建的相反顺序被破坏。原因是一个对象可能会使用另一个对象,因此依赖于它。考虑:

struct A { };

struct B {
 A &a;
 B(A& a) : a(a) { }
};

int main() {
    A a;
    B b(a);
}

If awere to destruct before bthen bwould hold an invalid member reference. By destructing the objects in the reverse order in which they were created we guarantee correct destruction.

如果a是之前破坏b,然后b将举行无效成员引用。通过以与创建对象相反的顺序销毁对象,我们保证了正确的销毁。

回答by Chris Jester-Young

Yes and yes. The order of destruction is always opposite to the order of construction, for member variables.

是的,是的。对于成员变量,析构顺序总是与构造顺序相反。