I see from this entry that virtual inheritance adds sizeof(pointer) to an object's memory footprint. Other than that, are there any drawbacks to me just using virtual inheritance by default, and conventional inheritance only when needed? It seems like it'd lead to more future-proof class design, but maybe I'm missing some pitfall.
Asked
Active
Viewed 7,567 times
31
-
2The order of initialization of base classes becomes non obvious to most people. Thus maintenance cost increase. – Martin York Dec 28 '10 at 08:00
-
Virtual inheritance adds an internal pointer only on some implementations. Itanium ABI doesn't use internal pointers, only vptr. – curiousguy Sep 09 '15 at 03:07
2 Answers
27
The drawbacks are that
- All classes will have to initialize all its virtual bases all the time (e.g. if A is virtual base of B, and C derives from B, it also have to initialize A itself).
- You have to use more expensive
dynamic_cast
everywhere you use astatic_cast
(may or may not be the issue, depending on your system and whether your design requires it).
Point 1 alone makes it not worth it, since you can't hide your virtual bases. There is almost always a better way.

Alex B
- 82,554
- 44
- 203
- 280
-
Oh god (1) is horrible. Thanks! Could you be more explicit about (2)? Do you mean that the compiler throws an error if I try to use static_cast
(my_virtual_base_instance)? – SuperElectric Dec 28 '10 at 20:42 -
1@SuperElectric: why is (1) horrible? Would it be better if a base subobject were not initialised? ;) – Yttrill Dec 28 '10 at 23:12
-
5Comments like "it's not worth it" show a complete failure to understand the purpose of virtual bases: virtual inheritance must *always* be used when you're subclassing an abstraction. There is no workaround or other possibility, if you fail to use it you cannot use multiple subclassing of abstractions without going back and fixing your design error, thus breaking encapsulation. – Yttrill Dec 28 '10 at 23:14
-
@Yttril: Perhaps Alex is merely suggesting that it's "not worth it" to do diamond inheritance of classes with data, something which Greg's answer suggests is almost always avoidable. I may be missing something, though, and I freely admit to "a complete failure to understand" what you're on about! (Thank goodness I'm teaching myself lisp; I think c++ can do something to one's personality after a while.) – SuperElectric Dec 29 '10 at 00:37
-
2@Yttrill @SuperElectric By "it's not worth it" I meant getting yourself into a situation where you'd need multiple virtual inheritance. By this point your design is too complex. As for point (2), it won't compile with `static_cast`. – Alex B Dec 29 '10 at 01:35
-
@AlexB Nonsense; virtual inheritance isn't complex, it is very natural. – curiousguy Sep 13 '15 at 21:01
-
2@AlexB I don't understand (1). All classes don't initialize all its base all the time (non virtual base) ? – Pedro Reis Feb 16 '17 at 15:45
-
1@AlexB Ok, this means that C constructor has to explicitly initialize A (not only B). – Pedro Reis Feb 16 '17 at 15:57
-
@AlexB @Pedro Would you mind elaborating on why "you can't hide your virtual bases"? In [this](https://www.geeksforgeeks.org/multiple-inheritance-in-c/) example, though not being efficient, I think the `TA` does not know the existence of `Person`, thus follows the principle of encapsulation? – wlnirvana Jun 27 '18 at 07:26
15
In my experience, virtual inheritance (as opposed to virtual methods) is almost never needed. In C++ it's used to address the "diamond inheritance problem", which if you avoid multiple inheritance cannot actually happen.
I'm pretty sure that I've never encountered virtual inheritance outside C++ books, which includes both code I write and million+ line systems I maintain.

Greg Hewgill
- 951,095
- 183
- 1,149
- 1,285
-
The standard C++ I/O streams library uses virtual inheritance. But yeah, other than that it's generally pretty rare. – Charles Salvia Dec 28 '10 at 04:03
-
1
-
2Hmm... I already know what it's for (I'm thinking of using it), and I understand that it's rare (I code in c++), but the question posed was "what are the drawbacks of virtual inheritance". – SuperElectric Dec 28 '10 at 20:37
-
3Another possible answer is that virtual inheritance makes your code harder to read. For those people who don't regularly encounter virtual inheritance, they will have to learn about it and apply the extra cognitive load of remembering what it means whenever they read your code. For those who *do* understand it, they will wonder why you are using an unnecessary feature if none of your classes actually use diamond inheritance. The [YAGNI](http://en.wikipedia.org/wiki/You_ain't_gonna_need_it) principle applies in this case. – Greg Hewgill Dec 28 '10 at 20:56