23

Problem:

class Base {
public:
  Base(Base* pParent);
  /* implements basic stuff */
};

class A : virtual public Base {
public:
  A(A* pParent) : Base(pParent) {}
  /* ... */
};

class B : virtual public Base {
public:
  B(B* pParent) : Base(pParent) {}
  /* ... */
};

class C : public A, public B {
public:
  C(C* pParent) : A(pParent), B(pParent) {} // - Compilation error here
  /* ... */
};

At the position given, gcc complains that it cannot match function call to Base(), i.e. the default constructor. But C doesn't inherit directly from Base, only through A and B. So why does gcc complain here?

Ideas? TIA /Rob

2
  • Compilation is done with no-rtti set, might be a problem? Commented Jan 24, 2010 at 9:05
  • 6
    No, inheritance is a compile-time thing, RTTI will not be required. Commented Jan 24, 2010 at 9:09

3 Answers 3

48

virtual base classes are special in that they are initialized by the most derived class and not by any intermediate base classes that inherits from the virtual base. Which of the potential multiple initializers would the correct choice for initializing the one base?

If the most derived class being constructed does not list it in its member initalization list then the virtual base class is initialized with its default constructor which must exist and be accessible.

Note that a virtual base identifier is allowed to be use in a constructor's initializer list even if it is not a direct base of the class in question.

Sign up to request clarification or add additional context in comments.

2 Comments

Addendum: Note that an abstract class cannot ever initialize its virtual base, thus need not call the virtual bases ctor in its ctors init-list even if no default-ctor exists.
So this means A and B's initialization of the parent class (i.e. ' : Base(pParent)') are both ignored when constructing the more derived class?
9

You need to explicitly call the constructor for Base from C:

class C : public A, public B {
public:
C(C* pParent) : Base(pParent), A(pParent), B(pParent) {}
/*... */
};

5 Comments

I didn't know you could do this. Does this mean that the compiler ignores the code in the c'tors of A and B where they seem to initialize Base?
No only can you do this, you must do it. No code is being ignored. The constructor for Base will only be called once, then the body of the constructors for A and B, then the body of the constructor for C.
I now understand the order of the constructor calls and the reasons behind this, except I usually have a base class with only a default constructor, so I don't have to have explicit calls to Base(). Surely, in the example from the OP, the compilers must still ignore the programmers indication that Base(pParent) should be called directly from the c'tors of both A and B?
Actually I think its the case that the compiler, seeing that virtual inheritance is involved, secretly makes several different constructors available to itself, and knowing every case where the most derived c'tor is being called, always calls the right sequence of constructors.
Correct.Virtual inheritance introduces a layer of indirection so that shared base classes are only constructed and allocated once.
5

If you declare a custom constructor, the default constructor is disabled. In virtual inheritance you need to call the virtually inherited constructor directly because otherwise it would not know whether to initialize by A or by B.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.