tags:

views:

106

answers:

4
+1  Q: 

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.

+2  A: 

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.

Reed Copsey
What was the order if B wasn't derived class of A?
Sanich
@Sanich: There'd be one less "A" in the initialization, and one less "A" in the destruction. Otherwise, order would be the same.
Reed Copsey
@Reed Copsey: Is it true that if B wasn't derived classes of A, the oredr of the ctors would be: A, B , C or it would be: B,A,C?
Sanich
@Sanich: A, B, C. It goes "base class", "members", "this"
Reed Copsey
@Sanich: If B had an A member, then it would be B, A, C. If you simply remove the derivation, then there is no A any more so it would go B, C.
Potatoswatter
@Reed Copsey: So the "base class" always called first no matter his order in the init. list? Even if the ctor of the base class not written in the init. list it will first call to the default one of the base class.
Sanich
@Sanich: Yes. Base class construction first, always.
Reed Copsey
A: 

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.

anand.arumug
+4  A: 

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.
sth
And to complete: in order to build a `C` you first build `A` (its base class) which outputs `ctor A`, then `B` (its member) which ouputs `ctor A` and `ctor B` (in this order) and finally execute the body of C's constructor which ouputs `ctor C`, giving `A A B C`.
Matthieu M.
In fact, some compilers (gcc, for one) will warn you about an initialization list order that differs from the declaration order.
Tyler McHenry
A: 

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

01d