Why another answer?
Well, many posts on SO and articles outside say, that diamond problem is solved by creating single instance of A
instead of two (one for each parent of D
), thus resolving ambiguity. However, this didn't give me comprehensive understanding of process, I ended up with even more questions like
- what if
B
and C
tries to create different instances of A
e.g. calling parametrized constructor with different parameters (D::D(int x, int y): C(x), B(y) {}
)? Which instance of A
will be chosen to become part of D
?
- what if I use non-virtual inheritance for
B
, but virtual one for C
? Is it enough for creating single instance of A
in D
?
- should I always use virtual inheritance by default from now on as preventive measure since it solves possible diamond problem with minor performance cost and no other drawbacks?
Not being able to predict behavior without trying code samples means not understanding the concept. Below is what helped me to wrap head around virtual inheritance.
Double A
First, lets start with this code without virtual inheritance:
#include<iostream>
using namespace std;
class A {
public:
A() { cout << "A::A() "; }
A(int x) : m_x(x) { cout << "A::A(" << x << ") "; }
int getX() const { return m_x; }
private:
int m_x = 42;
};
class B : public A {
public:
B(int x):A(x) { cout << "B::B(" << x << ") "; }
};
class C : public A {
public:
C(int x):A(x) { cout << "C::C(" << x << ") "; }
};
class D : public C, public B {
public:
D(int x, int y): C(x), B(y) {
cout << "D::D(" << x << ", " << y << ") "; }
};
int main() {
cout << "Create b(2): " << endl;
B b(2); cout << endl << endl;
cout << "Create c(3): " << endl;
C c(3); cout << endl << endl;
cout << "Create d(2,3): " << endl;
D d(2, 3); cout << endl << endl;
// error: request for member 'getX' is ambiguous
//cout << "d.getX() = " << d.getX() << endl;
// error: 'A' is an ambiguous base of 'D'
//cout << "d.A::getX() = " << d.A::getX() << endl;
cout << "d.B::getX() = " << d.B::getX() << endl;
cout << "d.C::getX() = " << d.C::getX() << endl;
}
Lets go through output. Executing B b(2);
creates A(2)
as expected, same for C c(3);
:
Create b(2):
A::A(2) B::B(2)
Create c(3):
A::A(3) C::C(3)
D d(2, 3);
needs both B
and C
, each of them creating its own A
, so we have double A
in d
:
Create d(2,3):
A::A(2) C::C(2) A::A(3) B::B(3) D::D(2, 3)
That's the reason for d.getX()
to cause compilation error as compiler can't choose which A
instance it should call method for. Still it's possible to call methods directly for chosen parent class:
d.B::getX() = 3
d.C::getX() = 2
Virtuality
Now lets add virtual inheritance. Using same code sample with the following changes:
class B : virtual public A
...
class C : virtual public A
...
cout << "d.getX() = " << d.getX() << endl; //uncommented
cout << "d.A::getX() = " << d.A::getX() << endl; //uncommented
...
Lets jump to creation of d
:
Create d(2,3):
A::A() C::C(2) B::B(3) D::D(2, 3)
You can see, A
is created with default constructor ignoring parameters passed from constructors of B
and C
. As ambiguity is gone, all calls to getX()
return the same value:
d.getX() = 42
d.A::getX() = 42
d.B::getX() = 42
d.C::getX() = 42
But what if we want to call parametrized constructor for A
? It can be done by explicitly calling it from constructor of D
:
D(int x, int y, int z): A(x), C(y), B(z)
Normally, class can explicitly use constructors of direct parents only, but there is an exclusion for virtual inheritance case. Discovering this rule "clicked" for me and helped understanding virtual interfaces a lot:
Code class B: virtual A
means, that any class inherited from B
is now responsible for creating A
by itself, since B
isn't going to do it automatically.
With this statement in mind it's easy to answer all questions I had:
- During
D
creation neither B
nor C
is responsible for parameters of A
, it's totally up to D
only.
C
will delegate creation of A
to D
, but B
will create its own instance of A
thus bringing diamond problem back
- Defining base class parameters in grandchild class rather than direct child isn't a good practice, so it should be tolerated when diamond problem exists and this measure is unavoidable.