Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 761
quazeeee Avatar asked Aug 24 '16 14:08

quazeeee


1 Answers

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.

like image 79
Pete Becker Avatar answered Sep 20 '22 09:09

Pete Becker