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

s4y
  • 50,525
  • 12
  • 70
  • 98
Robert
  • 2,330
  • 29
  • 47

3 Answers3

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.

CB Bailey
  • 755,051
  • 104
  • 632
  • 656
  • 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. – Deduplicator Sep 15 '14 at 18:12
  • 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? – Chris Jan 09 '15 at 18:16
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) {}
/*... */
};
BenG
  • 1,292
  • 8
  • 11
  • 2
    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`? – quamrana Mar 14 '15 at 21:52
  • 3
    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. – BenG Mar 15 '15 at 17:11
  • 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`? – quamrana Mar 16 '15 at 10:04
  • 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. – quamrana Mar 16 '15 at 10:11
  • Correct.Virtual inheritance introduces a layer of indirection so that shared base classes are only constructed and allocated once. – BenG Mar 17 '15 at 02:02
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.

Tronic
  • 10,250
  • 2
  • 41
  • 53