DEV Community

Discussion on: What is virtual inheritance in C++ and when should you use it?

Collapse
 
pgradot profile image
Pierre Gradot

Nice article!

I have never used virtual inheritance in real life.

I assume this would indicate a design defect, most of the time.

Also, adding virtual inheritance to solve the diamond problem somehow bothers me: as you are using two classes and run into the diamond problem, you have to modify them, but:

  • If there are from a library, you may not be able to modify the code.
  • If you can modify it, other users of the classes will pay for virtual inheritance even if they don't need it.

That's being said, I guess virtual inheritance should be used right from the beginning of the design of the classes, to solve a very particular business case (the diamond problem being then just a technical detail). Adding virtual inheritance seems like an ugly patch to me.

I may reconsider my point of view once I use it in real life :)

Collapse
 
sandordargo profile image
Sandor Dargo

Indeed, as said, in most of the languages huge and complex applications can be implemented without the need for virtual inheritance. I think it's worth to know about it and probably best to avoid it in your design.

Collapse
 
pgradot profile image
Pierre Gradot

Have you ever used it in real-life code?