How the compilers implement the virtual inheritance?
In the following code:
class A {
public:
A(int) {}
};
class B : public virtual A {
public:
B() : A(1) {}
};
class C : public B {
public:
C() : A(3), B() {}
};
Does a compiler generate two instance of B::ctor function, one without A(1) call, and one with it? So when B::constructor is called from derived class’s constructor the first instance is used, otherwise the second.
It’s implementation-dependent. GCC (see this question), for example, will emit two constructors, one with a call to
A(1), another one without.When B is constructed, the “full” version is called:
When C is constructed, the base version is called instead:
In fact, two constructors will be emitted even if there is no virtual inheritance, and they will be identical.