C++ 使用公共函数签名实现多个接口的派生类

C++ 使用公共函数签名实现多个接口的派生类,c++,class,inheritance,multiple-inheritance,C++,Class,Inheritance,Multiple Inheritance,当我试图编译我的代码时,我得到了一个编译错误。 错误是: multi.cc: In function ‘int main()’: multi.cc:35: error: cannot declare variable ‘mdc’ to be of abstract type ‘MostDerivedClass’ multi.cc:27: note: because the following virtual functions are pure within ‘MostDerivedClas

当我试图编译我的代码时,我得到了一个编译错误。 错误是:

multi.cc: In function ‘int main()’:
multi.cc:35: error: cannot declare variable ‘mdc’ to be of abstract type ‘MostDerivedClass’
multi.cc:27: note:   because the following virtual functions are pure within ‘MostDerivedClass’:
multi.cc:13: note:  virtual int Interface2::common_func()
multi.cc:36: error: request for member ‘common_func’ is ambiguous
multi.cc:13: error: candidates are: virtual int Interface2::common_func()
multi.cc:21: error:                 virtual int InterimClass::common_func()
这是我的代码:

class Interface1 {
public:
    virtual int common_func() = 0;
    virtual ~Interface1() {};
};

class Interface2 {
public:
    virtual int common_func() = 0;
    virtual int new_func() = 0;
    virtual ~Interface2() {};
};


class InterimClass : public Interface1 {
public:
    virtual int common_func() {
        return 10;
    }
};


class MostDerivedClass : public InterimClass, public Interface2 {
public:
    virtual int new_func() {
        return 20;
    }   
};

int main() {
    MostDerivedClass mdc;
    int x = mdc.common_func();
    cout << "The value = " << x << endl;    

    Interface2 &subset_of_funcs = dynamic_cast<Interface2 &>(mdc);
    x = subset_of_funcs.common_func();
}
类接口1{
公众:
虚int公共函数()=0;
虚拟~Interface1(){};
};
类接口2{
公众:
虚int公共函数()=0;
虚拟int new_func()=0;
虚拟~Interface2(){};
};
类间类:公共接口1{
公众:
虚int公共函数(){
返回10;
}
};
类MostDerivedClass:公共InterimClass,公共接口2{
公众:
虚拟int new_func(){
返回20;
}   
};
int main(){
最高级mdc;
int x=mdc.common_func();

您是否需要在
MostDerivedClass
中定义一个
common_func()
来满足您从
Interface2
继承的需求

你可以试试类似的东西

virtual int common_func() {
    return InterimClass::common_func();
}
如果无法更改第一个
接口1

如果你想在类之间建立真正的继承关系,你需要遵循Lol4t0建议。从
Interface1
提取一个超类,并为这个新创建的类创建
Interface2
子类。示例:

class RootInterface{
public :
    virtual int common_func() = 0;
    virtual ~RootInterface(){}
};

class Interface1 : public virtual RootInterface{
public:
    virtual ~Interface1() {};
};

class Interface2 : public virtual RootInterface{
    public:
    virtual int new_func() = 0;
    virtual ~Interface2() {};
};

class InterimClass : public Interface1 {
    public:
    virtual int common_func() {
        return 10;
    }
};

class MostDerivedClass : public InterimClass, public Interface2 {
public:
    virtual int new_func() {
        return 20;
    }
};

在MostDerivedClass中添加一个重写,并从中调用InterimClass::common_func()。

首先,我不太理解您的代码的含义

您需要知道,只实现了Interface1::common_func

为什么不让Interface2从Interface1继承?我想您希望两个常用的函数方法相等

示例代码(使用多态性):

类接口1
{
公众:
虚int公共函数()=0;
虚拟~Interface1(){};
};
类接口2:公共接口1{
公众:
虚int公共函数()=0;
虚拟int new_func()=0;
虚拟~Interface2(){};
};
类间类:公共接口2{
公众:
虚int公共函数(){
返回10;
}
};
类MostDerivedClass:公共InterimClass{
公众:
虚拟int new_func(){
返回20;
}
};
int test_func()
{
接口1*i1=新的MostDerivedClass;
int x=i1->common_func();

不能从第一个接口派生第二个接口,从第二个接口中删除
virtual int common_func()=0;
的声明,并使用关键字virtual引导编译器实现

class Interface1 {
public:
    virtual int common_func() = 0;
    virtual ~Interface1() {};
};

class BaseClass : public virtual Interface1 {
public:
    virtual int common_func() {
        return 10;
    }
};

class Interface2 : public virtual Interface1{
public:
    virtual int new_func() = 0;
    virtual ~Interface2() {};
};

class DerivedClass : public virtual BaseClass, public virtual Interface2 {
public:
    virtual int new_func() {
        return 20;
    }   
};

您的公用函数实际上不是“公用的”。
Interface1::common_func()
Interface2::common_func()没有任何共同之处
。如果您想要真正的通用函数,您应该从
Interface1
派生
Interface2
。然后应用@juanchopanza答案。感谢您的回答。我只是对现有代码的更改犹豫不决。希望以后某个时候,我可以重构代码以反映新的继承结构。
class Interface1 {
public:
    virtual int common_func() = 0;
    virtual ~Interface1() {};
};

class BaseClass : public virtual Interface1 {
public:
    virtual int common_func() {
        return 10;
    }
};

class Interface2 : public virtual Interface1{
public:
    virtual int new_func() = 0;
    virtual ~Interface2() {};
};

class DerivedClass : public virtual BaseClass, public virtual Interface2 {
public:
    virtual int new_func() {
        return 20;
    }   
};