I've started learning about virtual inheritance (and how it may solve problems of having a class derived from two parent classes with same parent). To better understand the mechanism behind it, i made a following example:
class A {
public:
A(string text = "Constructor A") { cout << text << endl; }
};
class B: public A {
public:
B(): A("A called from B") { cout << "Constructor B" << endl; }
};
class C : virtual public A {
public:
C() : A("A called from C") { cout << "Constructor C" << endl; }
};
class D : public B, public C {
public:
D() { cout << "Constructor D" << endl; }
};
I have class A
, class B
derived from A
, class C
virtually derived from A
, and class D
derived from B
and C
. In main i just form an object of class D
: D d;
and i get the following output
Constructor A
A called from B
Constructor B
Constructor C
Constructor D
What bothers me is, why is there "Constructor A" signalizing that it is not called by either class B
or C
. And why is there not "A called from C" before "Constructor C". For the latter one i know it has to do with the class C
being virtually derived so i guess it does not call Constructor A
again since object from class A
has already been formed (twice in fact).
EDIT:
In main i just make one object type D.
int main() {
D d;
}