15

Is possible to have virtual inheritance for class not providing default constructor?

The present diamond diagram (the simplest one with the only change of no default constructor provided) does not compile (g++ 4.4.3).

class A {
 public: 
  A(int ) {}
};
class B : virtual public A {
 public:
  B(int i) : A(i) {}
};
class C : virtual public A {
 public:
  C(int i) : A(i) {}
};
class D : public B, public C {
 public:
  D(int i) : B(i), C(i) {}
};

Thanks, Francesco

fbasile
  • 273
  • 3
  • 9

4 Answers4

24

You need to call A's constructor explicitly here

 D(int i) : A(i), B(i), C(i) {}

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.

Shamelessly copied from here :-)

Community
  • 1
  • 1
Prasoon Saurav
  • 91,295
  • 49
  • 239
  • 345
3

I believe your class D also needs to explicitly call A's constructor in its initializer list.

Mark B
  • 95,107
  • 10
  • 109
  • 188
1

The Dr. Dobbs article Multiple Inheritance Considered Useful explains various ways of dealing with this. The recommendation is basically to provide default constructors and init() methods. It adds more work for B and C but prevents D from having to know about A.

xan
  • 7,511
  • 2
  • 32
  • 45
  • 1
    Leaving D unaware of A is, of course, desirable. The 'init()' solution, unfortunately, has a drawback: the constructor of A will be called twice (once each by B and C) which may have side effects (especially if static members are used). Also, if a developer creates a `class D : public A {}`, the 'init()' method may not be called at all, leaving the object in an uninitialized state. – Marste Aug 12 '15 at 13:05
0

you need explict call A's construct like this:

D(int i) : A(i), B(i), C(i) {}
BlackMamba
  • 10,054
  • 7
  • 44
  • 67