16

I'm in the process of developing a framework for our internal use and thought making all inheritance virtual would avoid the diamond problem for other developers inheriting from my classes.

I looked around and found a lot of discussion on using virtual inheritance WHEN faced with the diamond issue, but nothing about preempting it.

frostbite
  • 628
  • 1
  • 14
  • 27
  • 1
    What about initialization of base classes? A virtual base class has to be initialized by the most-derived type. – dyp Jun 09 '13 at 18:03
  • 4
    Not sure about any drawback, but surely you shouldn't aim to please people that create diamond problems - that's like aiming to please people who are masochists by having the sharp end of nails pointing up the toilet seat - rewards a small number of people, and is painful for everyone else... – Mats Petersson Jun 09 '13 at 18:03
  • http://stackoverflow.com/questions/6620497/why-is-single-virtual-inheritance-not-enough-to-resolve-the-dreaded-diamond-prob#6620633 – Nathan Jun 09 '13 at 18:05
  • 2
    You can't use a `static_cast` to convert to a derived class pointer if there's virtual inheritance involved. (I'll keep posting comments until I've enough for an answer.) – dyp Jun 09 '13 at 18:06
  • thanks all (specially @aschepler and @DyP) for the quick and lucid response. This question arose as part of a feeble attempt to "Javafy" my framework. I'll stick to disabling inheritance and forcing encapsulation where applicable. – frostbite Jun 09 '13 at 19:29

2 Answers2

13

Remember that with non-virtual inheritance, each constructor calls the constructors of just immediate base classes. But with virtual inheritance, the most derived class needs to call the constructors of all virtual base classes.

If your base classes need initialization, virtual inheritance will mean every class down the tree will need to correctly initialize it. And any class in the middle of the tree can't count on its own base class being initialized the way it would like.

aschepler
  • 70,891
  • 9
  • 107
  • 161
  • out of curiosity, with "If your base classes need initialization" you mean "initialization to a specific given value" or the possibility that the member it's not even instantiated until it will be used ? – user2384250 Jun 09 '13 at 18:56
8

Just what I found in the Standard:

  • A virtual base class is initialized by the most-derived type (see aschepler's answer).
  • You can't use a static_cast to convert to a derived class reference/pointer if there's virtual inheritance involved. [expr.static.cast]/2, 11
  • You can't use C-style casts ("Explicit type conversion (cast notation)") to convert to a derived class pointer/reference ([expr.cast]), at least the example in [expr.dynamic.cast]/9 says so. (oooh no C-style casts ;)
  • Copy/Move assignment and ctor cannot be trivial if there's a virtual base class. [class.copy]/12, 25
  • [class.copy]/28 "It is unspecified whether subobjects representing virtual base classes are assigned more than once by the implicitly-defined copy assignment operator."
  • You can't have constexpr ctors if there's a virtual base class. [dcl.constexpr]/4
  • There are other subtleties, such as pointer-to-member conversions [conv.mem]/2, and reusing storage via placement-new on this [basic.life]/5,6.

Depending on the implementation of virtual base classes, there might be other drawbacks.

Community
  • 1
  • 1
dyp
  • 38,334
  • 13
  • 112
  • 177