关于c ++:派生类实现具有公共函数签名的多个接口

Derived class implementing multiple interfaces with a common function signature

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

1
2
3
4
5
6
7
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()

这是我的代码:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
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();
}

我的问题:

  • 我该如何告诉编译器,公共的func()已经由interimclass实现,interimclass是mostderivedClass的基类?

  • 还有别的方法来解决我的问题吗?我真正想做的是能够从Interface2中调用CommonFunc。我正在使用一些遗留代码和大量接口1中的方法。在我的新代码中,我只想调用这些接口1函数的一小部分,外加一些我需要添加的函数。

  • 小精灵


    你需要在MostDerivedClass中定义一个common_func()来满足你对Interface2的继承。

    你可以试试

    1
    2
    3
    virtual int common_func() {
        return InterimClass::common_func();
    }

    如果您不能更改第一个Interface1,这是最有用的。

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

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    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;
        }
    };


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

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    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从interface1继承?我想你希望两种常用的方法都相等。

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

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    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;
    }


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