Search code examples
c++inheritancediamond-problem

c++ virtual inheritance difference


Given two classes with a common virtual base class:

class Base {};

class Derived1 : public virtual Base {};

class Derived2 : public virtual Base {};

Is there any difference between these two further derived classes?:

  • class Derived3 : public virtual  Base, public Derived1, public Derived2 {};
    
  • class Derived3 : public Derived1, public Derived2 {};
    

The first also derives directly from the virtual base class, but I think that has no effect, because it's shared with Derived1 and Derived2.


Solution

  • They say the same thing. The only difference is that if you removed public Derived1 and public Derived2 from both definitions of Derived3, the first one would still inherit from Base and the second one would not.

    EDIT: I haven't thought carefully about whether there is some weird cross-cast situation where the two would also behave differently, although I don't think there is.