virtual inheritance constructor order

I am trying to understand better the concept of virtual inheritance, and what are its perils.

I read in another post (Why is Default constructor called in virtual inheritance?) that it (= virtual inheritance) changes the order of constructor call (the "grandmother" is called first, while without virtual inheritance it doesn't).

So I tried the following to see that I got the idea (VS2013):

#define tracefunc printf(__FUNCTION__); printf("rn")
struct A
{
    A(){ tracefunc; }

};

struct B1 : public A
{
    B1(){ tracefunc; };
};

struct B2 : virtual public A
{
    B2() { tracefunc; };
};

struct C1 : public B1
{
    C1() { tracefunc; };
};

struct C2 : virtual public B2
{
    C2() { tracefunc; };
};

int _tmain(int argc, _TCHAR* argv[])
{
    A* pa1 = new C1();
    A* pa2 = new C2();
}

The output is:

A::A
B1::B1
C1::C1
A::A
B2::B2
C2::C2

Which is not what I expected (I expected the order of the 2 classes will be different).

What am I missing? Can someone explain or direct me to a source that explains it better?

Thanks!


In your example, your output is to be expected. Virtual inheritance comes into play in the instance when you have a class with multiple inheritance who's parent classes also inherit from the same class/type (ie the "diamond problem"). In your example, your classes might be set up to virtually inherit (if needed elsewhere in the code), but they don't necessarily 'virtually inherit' based on your example since none of the derived classes ( B1/B2/C1/C2 ) do more than inherit directly from A .

To expand, I've tweaked your example to explain a little more:

#include <cstdio>

#define tracefunc printf(__FUNCTION__); printf("rn")
struct A
{
    A() { tracefunc; }
    virtual void write() { tracefunc; }
    virtual void read() { tracefunc; }
};

struct B1 : public A
{
    B1() { tracefunc; };
    void read(){ tracefunc; }
};

struct C1 : public A
{
    C1() { tracefunc; };
    void write(){ tracefunc; }
};

struct B2 : virtual public A
{
    B2() { tracefunc; };
    void read(){ tracefunc; }
};

struct C2 : virtual public A
{
    C2() { tracefunc; };
    void write(){ tracefunc; }
};

// Z1 inherits from B1 and C1, both of which inherit from A; when a call is made to any
// of the base function (i.e. A::read or A::write) from the derived class, the call is
// ambiguous since B1 and C1 both have a 'copy' (i.e. vtable) for the A parent class.
struct Z1 : public B1, public C1
{
    Z1() { tracefunc; }
};

// Z2 inherits from B2 and C2, both of which inherit from A virtually; note that Z2 doesn't
// need to inherit virtually from B2 or C2. Since B2 and C2 both virtual inherit from A, when
// they are constructed, only 1 copy of the base A class is made and the vtable pointer info
// is "shared" between the 2 base objects (B2 and C2), and the calls are no longer ambiguous
struct Z2 : public B2, public C2
{
    Z2() { tracefunc; }
};


int _tmain(int argc, _TCHAR* argv[])
{
    // gets 2 "copies" of the 'A' base since 'B1' and 'C1' don't virtually inherit from 'A'
    Z1 z1;
    // gets only 1 "copy" of 'A' base since 'B2' and 'C2' virtualy inherit from 'A' and thus "share" the vtable pointer to the 'A' base
    Z2 z2;

    z1.write(); // ambiguous call to write (which one is it .. B1::write() (since B1 inherits from A) or A::write() ?)
    z1.read(); // ambiguous call to read (which one is it .. C1::read() (since C1 inherits from A) or A::read() ?)

    z2.write(); // not ambiguous: z2.write() calls C2::write() since it's "virtually mapped" to/from A::write() 
    z2.read(); // not ambiguous: z2.read() calls B2::read() since it's "virtually mapped" to/from A::read() 
    return 0;
}

While it might be "obvious" to us humans which call we intend to make in the case of the z1 variable, since B1 doesn't have a write method, I would "expect" the compiler to choose the C1::write method, but due to how the memory mapping of objects work, it presents a problem since the base copy of A in the C1 object might have different information (pointers/references/handles) than the copy of the A base in the B1 object (since there's technically 2 copies of the A base); thus a call to B1::read() { this->write(); } B1::read() { this->write(); } could give unexpected behaviour (though not undefined).

The virtual keyword on a base class specifier makes it explicit that other classes that virtually inherit from the same base type, shall only get 1 copy of the base type.

Note that the above code should fail to compile with compiler errors explaining the ambiguous calls for the z1 object. If you comment out the z1.write(); and z1.read(); lines the output (for me at least) is the following:

A::A
B1::B1
A::A
C1::C1
Z1::Z1
A::A
B2::B2
C2::C2
Z2::Z2
C2::write
B2::read

Note the 2 calls to the A ctor ( A::A ) before Z1 is constructed, while Z2 only has 1 call to the A constructor.

I recommend reading the following on virtual inheritance as it goes more in depth on some of the other pitfalls to take note of (like the fact that virtually inherited classes need to use the initialization list to make base class ctor calls, or that you should avoid using C-style casts when doing such a type of inheritance).

It also explains a little more to what you were initially alluding to with the constructor/destructor ordering, and more specifically how the ordering is done when using multiple virtual inheritance.

Hope that can help clear things up a bit.


The output of compiler is right. In fact, this is about the goal of virtual inheritance. Virtual inheritance is aimed to solve the 'Diamond problem' in the multiple inheritance. For example, B inherits from A, C inherits from A and D inherits from B, C. The diagram is like this:

    A
   | |
   B C
   | |
    D

So, D has two instances A from B and C. If A has virtual functions, there is a problem.

For example:

struct A
{
    virtual void foo(){__builtin_printf("A");}
    virtual void bar(){}
};

struct B : A
{
    virtual void foo(){__builtin_printf("B");}
};

struct C : A
{
    virtual void bar(){}
};

struct D : B, C
{

};

int main()
{
    D d;
    d.foo(); // Error
}

If I use my xlC compiler to compile and run:

xlC -+ a.C

The error message is like that:

a.C:25:7: error: member 'foo' found in multiple base classes of different types
    d.foo(); // Error
      ^
a.C:9:18: note: member found by ambiguous name lookup
    virtual void foo(){__builtin_printf("B");}
                 ^
a.C:3:18: note: member found by ambiguous name lookup
    virtual void foo(){__builtin_printf("A");}
                 ^
1 error generated.
Error while processing a.C.

The error message is very clear, member 'foo' found in multiple base classes of different types. If we add virtual inheritance, the problem is solved. Because the construction rights of A is handled by D, there is one instance of A.

Back to your code, the inheritance diagram is like this:

A     A
|     |
B1    B2
|     |
C1    C2

There is no 'diamond problem', this is only single inheritance. So, the construction order is also A->B2->C2, there is no difference of output.


You won't be able to see any difference in the output because the output will be same in any of the following class hiearchies:

Hiearchy 1

class A {};

class  B2 : virtual public A {};

class  C2 : virtual public B2 {};

Hiearchy 2

class A {};

class  B2 : public A {};

class  C2 : virtual public B2 {};

Hiearchy 3

class A {};

class  B2 : virtual public A {};

class  C2 : public B2 {};

Hiearchy 3

class A {};

class  B2 : public A {};

class  C2 : public B2 {};

In all these case, A::A() will be executed first, followed by B2::B2() , and then C2::C2() .

The difference between them is when does A::A() get called. Does it get called from B2::B2() , or C2::C2() ?

I am not 100% clear on the answer for Hiearchy 1 . I think B2::B2() should get called from C2::C2 since B2 is a virtual base class of C . A::A() should get called from B2:B2() since A is a virtual base class of B2 . But I could be wrong on the exact order.

In Hierarchy 2 , A::A() will be called from B2::B2() . Since B2 is the virtual base class of C2 , B2::B2() gets called from C2::C2() . Since A is a normal base class of B2 , A::A() gets called from B2::B2() .

In Hierarchy 2 , A::A() will be called from C2::C2() . Since A is a virtual base class, A::A() gets called from C2::C2() . B2::B2() gets called after the call to A::A() is completed.

In Hierarchy 4 , A::A() will be called from B2::B2() . I think this case needs no explanation.

To clarify my doubt regarding Hiearchy 1 , I used the following program:

#include <iostream>

class A
{
   public:
      A(char const *from) { std::cout << "Called from : " << from << std::endl; }

};

class  B2 : virtual public A
{
   public:
      B2() : A("B2::B2()") {}
};

class  C2 : virtual public B2
{
   public:
      C2() : A("C2::C2()") {}
};

int main()
{
   C2 c;
}

I got the following output:

Called from : C2::C2()

This confirms what @TC indicated in his comment, which is different from what I had expected. A::A() gets called from C2::C2 , not from B2::B2 .

链接地址: http://www.djcxy.com/p/26708.html

上一篇: 使用重写策略时,Coq找不到子项

下一篇: 虚拟继承构造函数的顺序