【问题标题】:Derived class implementing multiple interfaces with a common function signature派生类实现具有通用函数签名的多个接口
【发布时间】:2012-08-09 02:56:30
【问题描述】:

我在尝试编译代码时遇到编译错误。 错误是这样的:

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() 已经由作为 MostDerivedClass 基类的 InterimClass 实现?

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

【问题讨论】:

  • 您的常用功能实际上并不“常用”。 Interface1::common_func()Interface2::common_func() 没有任何共同之处。如果你想要真正通用的功能,你应该从Interface1 派生Interface2。然后申请@juanchopanza 答案。

标签: c++ class inheritance multiple-inheritance


【解决方案1】:

让第二个接口派生自第一个接口,从第二个接口中去掉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;
    }   
};

【讨论】:

    【解决方案2】:

    首先,我不太明白你的代码的意思。

    你需要知道只有 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】:

    无论如何,您都需要在MostDerivedClass 中定义一个common_func() 以满足您从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;
        }
    };
    

    【讨论】:

      【解决方案4】:

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

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2021-04-12
        • 1970-01-01
        • 2023-03-08
        • 2010-09-22
        • 1970-01-01
        • 1970-01-01
        • 2017-12-26
        • 1970-01-01
        相关资源
        最近更新 更多