派生类实现具有公共函数签名的多个接口 [英] Derived class implementing multiple interfaces with a common function signature

查看:123
本文介绍了派生类实现具有公共函数签名的多个接口的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我尝试编译我的代码时,我收到一个编译错误。
错误是:

I'm getting a compile error when I try to compile my code. The error is this:

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();
}

我的问题:


  • 我如何告诉编译器common_func()已经由InterimClass实现,它是MostDerivedClass的基类?

  • How do I tell the compiler that common_func() is already implemented by the InterimClass which is a base class of MostDerivedClass?

有另一种方法来解决我的问题吗?我真正想做的是能够从Interface2调用common_func。我正在使用一些遗留代码与大量的方法在Interface1。在我的新代码中,我只想调用这些Interface1函数的一小部分,再加上我需要添加的几个。

Is there another way to fix my problem? What I would really like to do is to be able to also call common_func from Interface2. I'm working with some legacy code with a huge amount of methods in Interface1. In my new code, I only want to call a small set of these Interface1 functions, plus a few that I need to add.

推荐答案

您需要在 MostDerivedClass common_func() >以满足您从 Interface2

You need to define a common_func() anyway in MostDerivedClass to satisfy your inheritance from Interface2

的继承,您可以尝试类似

you can try something like

virtual int common_func() {
    return InterimClass::common_func();
}



如果您不能更改第一个 Interface1

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

If you want a real inheritance relationship between your classes you need to follow Lol4t0 advice. Extract a superclass from Interface1, and make Interface2 subclass of this newly created class. Example :

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;
    }
};

这篇关于派生类实现具有公共函数签名的多个接口的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆