views:

190

answers:

6

I understand the basics of C++ virtual inheritance. However, I'm confused about where exactly I need to use the virtual keyword with a complex class hierarchy. For example, suppose I have the following classes:

            A
           / \
          B   C
         / \ / \
        D   E   F
         \ / \ /
          G   H
           \ /
            I

If I want to ensure that none of the classes appear more than once in any of the subclasses, which base classes need to be marked virtual? All of them? Or is it sufficient to use it only on those classes that derive directly from a class that may otherwise have multiple instances (i.e. B, C, D, E and F; and G and H (but only with the base class E, not with the base classes D and F))?

+2  A: 

My personal suggestion would be to start at B and C : virtual A, and then keep adding until the compiler stops complaining.

In reality, I'd say that B and C : virtual A, G and H : virtual E, and E : virtual B and C. All the other inheritance links can be normal inheritance. This monstrosity would take like six decades to make a virtual call, though.

DeadMG
You also have to `F: virtual C` and `D: virtual B`, otherwise H would inherit virtualy of C through E, and non-virtualy from F.
Didier Trosset
Can you explain why only those need to be virtual? Why not D : virtual B and F : virtual C? And why would virtual function calls be slow on such a class?
jchl
You're right, those probably need to be virtual too. You really need to check it with your compiler. As for slow, because there's six dozen different types the compiler needs to check for each virtual function call.
DeadMG
+5  A: 

You have to specify virtual inheritance when inheriting from any of A, B, C, and E classes (that are at the top of a diamond).

class A;
class B: virtual A;
class C: virtual A;
class D: virtual B;
class E: virtual B, virtual C;
class F: virtual C;
class G:         D, virtual E;
class H: virtual E,         F;
class I:         G,         H;
Didier Trosset
Thanks, that makes sense now. As a follow-on question, can you explain what (if anything) would be the effect of using virtual inheritance everwhere? Nothing? Or would I end up with a different (larger) layout for instances of I?
jchl
Using virtual inheritance everywhere slows things down, a bit (I have never measured though). The main drawback of virtual inheritance IMHO, is that once you've cast your object into a pointer or reference to one of its virtual base, you cannot cast it back to the inheriting class.
Didier Trosset
I didn't realize that you couldn't downcast a pointer to a virtual base class. Thanks for pointing (no pun intended) that out.
jchl
Err, just to be sure there's no misunderstanding. You can downcast a `D*` to an `A*`. But then, you cannot *upcast* back the `A*` to a `D*`.
Didier Trosset
A: 

If you want to have only one "physical" instance of each type for each instance of each type (only one A, only one B etc.) You'll just have to use virtual inheritance each time you use inheritance.

If you want separate instances of one of the types, use normal inheritance.

Klaim
The question is whether or not *all* the inheritance needs to be virtual to give the required heirarchy. The answer is no.
Mike Seymour
+14  A: 

I toyed a program together which could help you to study the intricacies of virtual bases. It prints the class hierarchy under I as a digraph suitable for graphiviz ( http://www.graphviz.org/ ). There's a counter for each instance which helps you to understand the construction order as well. Here's the programm:

#include <stdio.h>
int counter=0; 



#define CONN2(N,X,Y)\
    int id; N() { id=counter++; }\
    void conn() \
    {\
        printf("%s_%d->%s_%d\n",#N,this->id,#X,((X*)this)->id); \
        printf("%s_%d->%s_%d\n",#N,this->id,#Y,((Y*)this)->id); \
        X::conn(); \
        Y::conn();\
    }
#define CONN1(N,X)\
    int id; N() { id=counter++; }\
    void conn() \
    {\
        printf("%s_%d->%s_%d\n",#N,this->id,#X,((X*)this)->id); \
        X::conn(); \
    }

struct A { int id; A() { id=counter++; } void conn() {} };
struct B : A { CONN1(B,A) };
struct C : A { CONN1(C,A)  };
struct D : B { CONN1(D,B) };
struct E : B,C { CONN2(E,B,C) };
struct F : C { CONN1(F,C) };
struct G : D,E { CONN2(G,D,E) };
struct H : E,F { CONN2(H,E,F) };
struct I : G,H { CONN2(I,G,H) };
int main()
{
    printf("digraph inh {\n");
    I i; 
    i.conn(); 
    printf("}\n");
}

If I run this (g++ base.cc ; ./a.out >h.dot ; dot -Tpng -o o.png h.dot ; display o.png), I get the typical non-virtual base tree: alt text

Adding enough virtualness...

struct B : virtual A { CONN1(B,A) };
struct C : virtual A { CONN1(C,A)  };
struct D : virtual B { CONN1(D,B) };
struct E : virtual B, virtual C { CONN2(E,B,C) };
struct F : virtual C { CONN1(F,C) };
struct G : D, virtual E { CONN2(G,D,E) };
struct H : virtual E,F { CONN2(H,E,F) };
struct I : G,H { CONN2(I,G,H) };

..results in the diamond shape (look at the numbers to learn the construction order!!)

alt text

But if you make all bases virtual:

struct A { int id; A() { id=counter++; } void conn() {} };
struct B : virtual A { CONN1(B,A) };
struct C : virtual A { CONN1(C,A)  };
struct D : virtual B { CONN1(D,B) };
struct E : virtual B, virtual C { CONN2(E,B,C) };
struct F : virtual C { CONN1(F,C) };
struct G : virtual D, virtual E { CONN2(G,D,E) };
struct H : virtual E, virtual F { CONN2(H,E,F) };
struct I : virtual G,virtual H { CONN2(I,G,H) };

You get a diamond with a different initialization order:

alt text

Have fun!

Luther Blissett
+1 Wow what an answer, I had to favorite this question just to make sure I had a link to this for future reference.
bshields
Not exactly the explanation I was looking for, but extremely cool nonetheless.
jchl
+10 if it was possible.. ;) - so unfortunately I can give +1 only.
IanH
+1  A: 

If you want to make sure that an object of the top class in the hierarchy (I in your case) contains exactly one subobject of each parent class, you have to find all classes in your hierarchy that have more than one superclass and make these classes virtual bases of their superclasses. That's it.

In your case classes A, B, C and E have to become virtual base classes every time you inherit from them in this hierarchy.

Classes D, F, G and H don't have to become virtual base classes.

AndreyT
A: 

Edited: I thought A was the most derived class ;)

@Luther's answer really cool, but back to the original question:

You NEED to use virtual inheritance when inheriting from any class from which at least one other class inherits in the inheritance hierarchy (in Luther's diagrams it means at least two arrows point to the class).

Here it's unnecessary before D, F, G and H because only one class derives from them (and none derives from I at the moment).

However, if you don't know beforehand whether or not another class will inherit from your base class, you can add in virtual as a precaution. For example it's recommended for an Exception class to inherit virtually from std::exception by no other than Stroustrup himself.

As Luther has noted, it modifies the instantiation order (and has a slight effect on performances) but I would consider any design relying on the construction order to be wrong to begin with. And just as a precision: you still have the guarantees that base classes are initialized before any attribute of the derived class, and therefore before the execution of the derived's constructor body.

Matthieu M.
My original question was unclear: class I (not A) is the most-derived class. So I think you mean (as others have said) that virtual is unnecessary before D, F, G and H (and I, though no-one derives from I).I don't care about the instantiation order; I _do_ care about object size. I'd like to avoid having unnecessary additional vtable pointers in each instance.
jchl
@jchl: Your have to keep in mind that virtual inheritance is normally implemented by embedding derived-to-base pointers into the object for each virtual derived-to-base link. In other words, you will not be really be saving much in terms of hidden household data by switching to virtual inheritance. On the contrary, with virtual inheritance the inner workings usually become a lot more complicated.
AndreyT