Construction Order of Derived Class Objects

When a derived class object is created using constructors, it is created in the following order:

  1. Virtual base classes are initialized, in the order they appear in the base list.
  2. base classes are initialized, in declaration order.
  3. members are initialized in declaration order (regardless of their order in the initialization list).
  4. body of the constructor is executed.

In the following code fragment, the constructor for class B1 is called before the member d1 is initialized. The value passed to the constructor for class B1 is undefined.

class B1
{
      int b;
public:
      B1();
      B1(int i) {b = i;}
};
class D : public B1
{
      int d1, d2;
public:
      D(int i, int j) : d1(i), B1(d1) {d2 = j;}
      // d1 is not initialized in call B1::B1(d1)
};


Constructors and Destructors Overview


Initializing Base Classes and Members
Initialization by Constructor
Constructors
Derivation