Default constructor and virtual inheritance

后端 未结 4 1857
无人共我
无人共我 2020-12-11 00:50

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

4条回答
  •  没有蜡笔的小新
    2020-12-11 01:42

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

提交回复
热议问题