Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Order of member constructor and destructor calls

Tags:

c++

People also ask

Which is called first constructor or destructor?

It is always called in the reverse order of the constructor. if a class is inherited by another class and both the classes have a destructor then the destructor of the child class is called first, followed by the destructor of the parent or base class.

What's the order of the calling of destructors?

The destructors will be called in the order s5 , s4 , s3 , s2 , s1 . This is a general rule: if two objects' lifetimes overlap, then the first to be constructed will be the last to be automatically destroyed.

What order class constructors are called?

The base class constructors are called in order of derivation—for example, if ClassA is derived from ClassB , which is derived from ClassC , the ClassC constructor is called first, then the ClassB constructor, then the ClassA constructor.

What is the order of constructor execution?

Constructors of Virtual base classes are executed, in the order that they appear in the base list. Constructors of nonvirtual base classes are executed, in the declaration order. Constructors of class members are executed in the declaration order (regardless of their order in the initialization list).


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

6 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, 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 ]


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


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 a were to destruct before b then b would hold an invalid member reference. By destructing the objects in the reverse order in which they were created we guarantee correct destruction.


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