Sanich
Sanich

Reputation: 1835

Constructors order

#include <iostream>
class A
{
public:
    A() { std::cout << " A ctor" << std::endl; }
    A(int i) { std::cout << " A ctor i" << std::endl; }
    ~A() { std::cout << " A dtor" << std::endl; }
};
class B: public A
{
public:
    B() : A () { std::cout << " B ctor" << std::endl; }
    ~B() { std::cout << " B dtor" << std::endl; }
};
class C: public A
{
public:
    B _b;

    C() : _b (), A () { std::cout << " C ctor" << std::endl; }
    ~C() { std::cout << " C dtor" << std::endl; }
};
int main ()
{
    C c;
}

The output is:

A ctor
A ctor
B ctor
C ctor
C dtor
B dtor
A dtor
A dtor

What is the order of the init. list? Why, in the init. list of C, ctor of A called before ctor of B? I thought the output should be:

A ctor
B ctor
A ctor
C ctor
C dtor
A dtor
B dtor
A dtor

Thanks.

Upvotes: 1

Views: 253

Answers (4)

01d
01d

Reputation: 55

If you use GNU compiler, -Wall option will help you.

Upvotes: 0

sth
sth

Reputation: 229593

The order in which you write initializations in the initialization list is not important, the order of initialization is determined independently of that list by other rules:

  • First the base class is initialized. That's why in the construction of C the base class constructor A is called first. Everything that belongs to the base class is constructed in this step (base classes and member variables belonging to the base class), just like when a normal object of that base class would be constructed.
  • Then the member variables of the derived class are initialized, in the order in which they are declared in the class. So if there are several member variables, the order in which they are declared determines the order in which they are initialized. The order of an initialization list is not important.

Upvotes: 4

yasouser
yasouser

Reputation: 5177

I think your confusion is why is C's initialization list processed right to left instead of left to write. It is because, the compiler processes parameters in "last in first out" fashion. Hence the order: First A's c'tor. Since _b is an object of B which is derived from A, base class is constructed before the derived class so A c'tor is called and then B's c'tor. And finally, C's c'tor is called. When C's object is destructed, it follows the reverse order.

Upvotes: 0

Reed Copsey
Reed Copsey

Reputation: 564413

Base class constructors are called before derived class constructors. This allows the derived class to use members in the base class during their construction.

During destruction, the opposite is true. Subclass destruction occurs before the base class, for exactly the same reason.

If you think about it - it makes perfect sense. The base class has no knowledge of the subclass, but the opposite is not true. This determines the ordering in order for everything to work as expected.

Upvotes: 2

Related Questions