tags:

views:

1433

answers:

6

I've never used it in the professional software even though in our shop, and others I have worked for, we design large scale systems. The only time I messed with virtual inheritance was during my interview in a company. Nonetheless, I played with it during afterhours.

Do you guys use it? Do you understand how it works in depth (how most popular compiler vendors implement it)? I would really like to know how it is used in professional software, if at all. Tricks and tips would be appreciated, too.

To me, virtual inheritance was just a solution to an infamous diamond problem. Hence, it never found its way in our software, as we don't have in our architecture MI resulting in the diamond.

Thanks.

+5  A: 

The main point with virtual inheritance is to prevent derived classes from inheriting multiple copies of different superior classes. This can occur in any case where there may be multiple inheritance -- as you correctly note, the "diamond problem", which is to say where the inheritance graph is a DAG instead of a strict tree.

The C++ FAQ goes into it in some detail. I'd also recommend the C++ FAQ Book; I used to work for the authors and they're quite good.

Charlie Martin
Dan
+2  A: 

I don't see why you'd want to use it if your architecture doesn't use multiple inheritance.

If you did happen to use MI I can't see why you wouldn't use virtual inheritance. There doesn't seem to be any drawback except for remembering to add the virutal keyword in the right places.

Here's two reasons: 1) virtual inheritance requires subclasses to be aware (and if you refactor virtual bases, it impacts all of your users needlessly). 2) virtual inheritance adds runtime cost to every constructor and destructor. Why pay that cost when it's almost never necessary?
Tom
Can you explain how refactoring virtual bases is different than refactoring normal bases in a diamond-inheritance situation?For the constructor cost, isn't it quicker because when using a non-virtual base class it needs to make multiple copies of the base whereas with virtual it's just pointers?
Another disadvantage of virtual inheritance is that you can no longer downcast from pointer-to-Base to pointer-to-Derived with static_cast<Derived*>. See this illuminating post for details: http://groups.google.com/group/comp.lang.c++.moderated/msg/631ac4837db0abbb
j_random_hacker
+5  A: 

I've never used it in the professional software even though in our shop,

Do you use iostream? We do. Look up the design of iostreams and you'll know you've been using virtual inheritance.

dirkgently
HAHAH, yeah indirectly we do use it :)
+1  A: 

Virtual inheritance can also be used to make a class a final class, i.e. to make it so that no other classes can derive from it. I grabbed that trick once from Stroustroup (http://www.research.att.com/~bs/bs_faq2.html#no-derivation).

phresnel
A: 

I've worked with several commercial libraries that required its use. For example, the library that Reuters supplied to access real-time financial market data used to require you to derive your listening classes from several base classes (don't know if it still does) and this needed virtual inheritance. It was no big deal, once you realised you had to use it - a fact not totally well documented.

anon
A: 

for gaining further insights regarding virtual inheritance you could also check out this post: http://cpptalk.wordpress.com/2009/08/11/constructor-selection-with-virtual-inheritance/ I have in-depth knoweldge of the mechanism, so if you have a specific question - just go ahead and ask.

rmn