简体   繁体   English

从C++中具有多重继承的类派生的类:派生类试图调用根基类构造函数

[英]Class derived from a class with multiple inheritance in C++: the derived class is trying to call the root base class constructor

I'm sorry if this feels like a cheap sequel to my last question.如果这感觉像是我上一个问题的廉价续集,很抱歉

I have a diamond inheritance where D is derived from both B and C , who in turn are both derived (virtually) from A .我有一个钻石继承,其中D源自BC ,而后者又(实际上)源自A A , B and C are abstract, and thanks to the answers to my previous questions the compiler is now aware of it and all is fine. ABC是抽象的,多亏了我之前问题的答案,编译器现在知道了,一切都很好。

Now, I need to create a class E derived from D .现在,我需要创建一个从D派生的类E As far as I know, normally the constructor E::E should call D::D , and it would be D::D 's job to call all of A::A , B::B , and C::C .据我所知,通常构造函数E::E应该调用D::D ,而调用所有A::AB::BC::CD::D的工作.

But my compiler really insists on having E::E call A::A itself.但是我的编译器确实坚持让E::E调用A::A本身。

Here is a simple example I made:这是我制作的一个简单示例:

class A {                       //abstract
    protected:
        A(int foo) {}
        virtual void f() =0;
};

class B: public virtual A {     // abstract
    protected:
        B() {}
};

class C: public virtual A {     // abstract
    protected:
        C() {}
};

class D: public B, public C {   // concrete
    public:
        D(int foo, int bar) :A(foo) {}
        void f() {}
};

class E: public D {             // concrete
    public:
        E(int foo, int bar, int buz) :D(foo, bar) {}
};


int main()
{
    return 0;
}

And here is the compilation error:这是编译错误:

$ g++ test.cpp
test.cpp: In constructor ‘E::E(int, int, int)’:
test.cpp:25:49: error: no matching function for call to ‘A::A()’
   25 |         E(int foo, int bar, int buz) :D(foo, bar) {}
      |                                                 ^
test.cpp:3:9: note: candidate: ‘A::A(int)’
    3 |         A(int foo) {}
      |         ^
test.cpp:3:9: note:   candidate expects 1 argument, 0 provided
test.cpp:1:7: note: candidate: ‘constexpr A::A(const A&)’
    1 | class A {                       //abstract
      |       ^
test.cpp:1:7: note:   candidate expects 1 argument, 0 provided
test.cpp:1:7: note: candidate: ‘constexpr A::A(A&&)’
test.cpp:1:7: note:   candidate expects 1 argument, 0 provided

I know the virtual inheritance is correct and I know the compiler knows which classes I intend to be abstract and which I intend to be instantiable, because if I remove class E , the code compiles.我知道虚拟继承是正确的,我知道编译器知道我打算抽象哪些类以及我打算实例化哪些类,因为如果我删除class E ,代码会编译。

What am I missing?我错过了什么?

But my compiler really insists on having E::E call A::A itself.但是我的编译器确实坚持让 E::E 调用 A::A 本身。

Like I explained in anwer to your previous question: "the constructor of the most derived class calls the constructor of the virtual base" .就像我在回答您之前的问题时所解释的那样: “最派生类的构造函数调用虚拟基类的构造函数”

All non-abstract classes in a hierarchy that contains virtual bases must correctly initialise the virtual bases because they can potentially be instantiated as the most derived class.包含虚拟基类的层次结构中的所有非抽象类都必须正确初始化虚拟基类,因为它们可能被实例化为最派生的类。 For example, if you create an instance of E , then it is the constructor of E that initialises the virtual base A .例如,如果您创建的实例E ,那么它的构造函数E是初始化虚拟基地A

In your code, the constructor of E attempts to use the default constructor of A by omitting the initialiser.在您的代码中, E的构造函数尝试通过省略初始化程序来使用A的默认构造函数。 But A is not default constructible, so the program is ill-formed.但是A不是默认可构造的,因此程序格式错误。

What am I missing?我错过了什么?

The initialiser for the virtual base A in the constructor of E . E的构造函数中虚基A的初始化器。

As far as I know, there is no way to delegate the construction to of the virtual base to another concrete base such as D .据我所知,无法将虚拟基础的构造委托给另一个具体基础,例如D

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 在C ++中派生类构造函数之后调用基类构造函数 - Call base class constructor after the derived class constructor in C++ c++ 中派生的 class 构造函数中的动态基 class 构造函数调用 - dynamic base class constructor call in derived class constructor in c++ C++派生类构造函数调用基类构造函数错误 - C++ derived class constructor call base class constructor errors C++ 派生模板 class 继承自模板基础 class,无法调用基础 ZA2F2ED4F8EBC2CBB4C21A29DC40AB6 构造函数 - C++ derived template class is inheriting from template base class, cannot call base class constructor C ++构造函数继承(从派生类调用构造函数) - C++ Constructor Inheritance (Invoking Constructor from Derived Class) 派生类中的基本构造函数调用 - Base Constructor Call in Derived Class 如何调用所有基类的复制构造函数来复制C ++中钻石继承中的大多数派生类对象? - How to call copy constructor of all base classes for copying most derived class object in diamond inheritance in C++? 是否可以从C ++的派生类中调用基类中的方法? - is it possible to call a method in the base class from a derived class in c++? C ++如何从基类调用派生类中的方法 - C++ how to call method in derived class from base class C ++继承,从基类调用派生函数 - C++ Inheritance, calling a derived function from the base class
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM