派生类实现具有公共函数签名的多个接口

时间:2012-08-12 18:44:23

标签: 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 ‘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();
}

我的问题:

  • 如何告诉编译器,作为MostDerivedClass基类的InterimClass已经实现了common_func()?

  • 还有其他方法可以解决我的问题吗?我真正想做的是能够从Interface2调用common_func。我正在使用Interface1中的大量方法处理一些遗留代码。在我的新代码中,我只想调用一小部分这些Interface1函数,以及我需要添加的一些函数。

4 个答案:

答案 0 :(得分:3)

您需要在common_func()中定义MostDerivedClass,以满足您对Interface2的继承

你可以试试像

这样的东西
virtual int common_func() {
    return InterimClass::common_func();
}

如果您无法更改第一个Interface1

,这将非常有用

如果您想在类之间建立真正的继承关系,则需要遵循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;
    }
};

答案 1 :(得分:0)

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

答案 2 :(得分:0)

首先,我并不真正理解您的代码意义。

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

为什么不让Interface2继承Interface1?我想你们希望两个common_func方法都是平等的。

示例代码(使用多态):

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

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

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

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

int test_func()
{
    Interface1 * i1 = new MostDerivedClass;
    int x = i1->common_func();
    cout << "The value = " << x << endl;

    Interface2 * i2 = new MostDerivedClass;
    x = i2->common_func();

    return 0;
}

答案 3 :(得分:0)

让第二个接口从第一个接口派生,从第二个接口删除virtual int common_func() = 0;的声明,&amp;使用关键字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;
    }   
};