views:

267

answers:

4

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  A: 

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
+12  A: 

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.

Charles Bailey
+1, Most precise answer :)
Prasoon Saurav
+4  A: 

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) {}
/*... */
};
BennyG
A: 

Thanks everyone, got it all working. Makes sense really, I just have an odd feeling that I did this once in VC++ and I didn't get these errors, but it is possible that my virtual base class in that case had just the default constructor.

Robert