Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there a downside to making ALL inheritance virtual?

Tags:

c++

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.

like image 580
frostbite Avatar asked Jun 09 '13 17:06

frostbite


People also ask

When should you use virtual inheritance?

Virtual inheritance is used when we are dealing with multiple inheritance but want to prevent multiple instances of same class appearing in inheritance hierarchy. From above example we can see that “A” is inherited two times in D means an object of class “D” will contain two attributes of “a” (D::C::a and D::B::a).

Should we always use virtual inheritance if yes why if not why not?

The answer is definitely no. The base of an idiomatic answer can be the most fundamental idea of C++: you only pay for what you use. And if you don't need virtual inheritance, you should rather not pay for it. Virtual inheritance is almost never needed.

What happens if we don't use virtual function in inheritance?

If you don't use virtual functions, you don't understand OOP yet. Because the virtual function is intimately bound with the concept of type, and type is at the core of object-oriented programming, there is no analog to the virtual function in a traditional procedural language.

What is the effect of using virtual inheritance in C++ in the context of multiple inheritance?

Virtual base class in C++ Virtual base classes are used in virtual inheritance in a way of preventing multiple “instances” of a given class appearing in an inheritance hierarchy when using multiple inheritances.

Which of the following statement is correct about virtual inheritance?

Which of the following statements about virtual inheritance is correct? Yes, we can access the base class from a derived class that is why there is no use of creating an object of the base class.

How does virtual inheritance solve the diamond problem?

Virtual inheritance solves the classic “Diamond Problem”. It ensures that the child class gets only a single instance of the common base class. In other words, the Snake class will have only one instance of the LivingThing class. The Animal and Reptile classes share this instance.


2 Answers

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.

like image 106
aschepler Avatar answered Nov 16 '22 01:11

aschepler


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.

like image 44
dyp Avatar answered Nov 16 '22 01:11

dyp