When a derived class object is created using constructors, it is created in the following order:
The following example demonstrates this:
#include <iostream>
using namespace std;
struct V {
V() { cout << "V()" << endl; }
};
struct V2 {
V2() { cout << "V2()" << endl; }
};
struct A {
A() { cout << "A()" << endl; }
};
struct B : virtual V {
B() { cout << "B()" << endl; }
};
struct C : B, virtual V2 {
C() { cout << "C()" << endl; }
};
struct D : C, virtual V {
A obj_A;
D() { cout << "D()" << endl; }
};
int main() {
D c;
}
The following is the output of the above example:
V()
V2()
B()
C()
A()
D()
The above output lists the order in which the C++ run time calls the constructors to create an object of type D.
Related information