简体   繁体   English

声明接口并使用C ++中的多继承实现接口

[英]Declare interface and Implement the interface using multi-inheritance in C++

I am wonder it is available to Declare interface and Implement the interface using multi-inheritance. 我想知道它可用于声明接口和使用多继承实现接口。

For example. 例如。

I Declare abstract class for interface. 我声明接口的抽象类。

class Interface{
public:
    virtual int interf1(int a) = 0;
    virtual int interf2(char c) = 0;
};

And than to implement these two interface, i define two classes for each, like below 除了实现这两个接口,我为每个接口定义了两个类,如下所示

class Implement1{
public:
    int interf1(int a){
        std::cout << a << a << std::endl;
        return 0;
    }
};

class Inplement2{
public:
    int interf2(char c){
        std::cout << c << c << c << std::endl;
        return 0;
    }
};

And then as Last phase, i define a class which inherit Interface and implement classes all. 然后作为最后一个阶段,我定义一个继承Interface并实现所有类的类。

class Test : public Interface, public Implement1, public Inplement2{

};

But of course, it is not compiled. 但是,当然,它不是编译的。

Is there any way to build this kind of functionality? 有什么办法可以建立这种功能?

And Actually i found some way while struggling to make it compiled like below, but i am not sure it is safe way which doesn't make potential error, even though now it seem to work since it is simple code. 而且实际上我在努力使它像下面那样编译时找到了某种方法,但是我不确定这是不会造成潜在错误的安全方法,即使现在它似乎很有效,因为它是简单的代码。

class Test : public Interface, private Implement1, private Inplement2{
public:
    virtual int interf1(int a){
        return Implement1::interf1(a);

    };
    virtual int interf2(char c){
        return Inplement2::interf2(c);
    }
};

No, there's no way to override a virtual function declared in a base class except by redeclaring it in a class derived from that base class. 不可以,除非在从该基类派生的类中重新声明虚函数,否则无法覆盖在基类中声明的虚函数。 You'll need to declare (and define) Test::interf1 and Test::interf2 as you've done in the final example. 正如您在最后一个示例中所做的那样,您需要声明(并定义) Test::interf1Test::interf2

This means there's no advantage to such weird tricks with inheritance to provide the implementation. 这意味着带有继承功能的怪异技巧无法提供实现。 I'd use a more straightforward composition: 我会使用更直接的组成:

class Test : public Interface {
public:
    virtual int interf1(int a){
        return i1.interf1(a);
    }
    virtual int interf2(char c){
        return i2.interf2(c);
    }
private:
    Implement1 i1;
    Inplement2 i2;
};

Another possibility is virtual inheritance: 另一个可能性是虚拟继承:

class Implement1 : public virtual Interface {
    // implement interf1 here
};
class Inplement2 : public virtual Interface {
    // implement interf2 here
};
class Test : public Implement1, public Inplement2 {
};

But I wouldn't recommend this without a good reason. 但是我不会在没有充分理由的情况下建议这样做。 It increases the coupling between the interface and the implementation, and is more likely to melt the brain of someone trying to figure out which functions are declared where. 它增加了接口和实现之间的耦合,并且更可能使试图弄清楚在哪里声明了哪些功能的人的大脑融化。

I suggest two alternatives. 我建议两种选择。

1. Split the interface 1.拆分界面

Define two interfaces, Interface1 with interf1 method, and Interface2 with interf2 method. 定义两个接口, Interface1使用interf1方法, Interface2使用interf2方法。 Define two implementations Implement1 and Implement2 which inherit from their corresponding (single) interface. 定义从其对应的(单个)接口继承的两个实现Implement1Implement2 Combine those by inheriting from both Implement1 and Implement2 . 通过继承Implement1Implement2合并它们。

The problem is that you then can't have a common interface to use as a polymorphic interface type. 问题在于您将无法使用通用接口作为多态接口类型。

2. Use virtual inheritance for the interface 2.对接口使用虚拟继承

Live demo 现场演示

Keep your original Interface definition. 保留原始的Interface定义。 The implementations both should virtually derive from the interface: class Implement1 : virtual public Interface { ... }; 两种实现都应该从接口虚拟地派生: class Implement1 : virtual public Interface { ... }; . Then, when combining those implementations, don't derive from the interface again but just from your two implementations. 然后,在组合这些实现时,不要再次从接口中派生,而只是从您的两个实现中派生。 Their super-class Interface is going to be merged to a single one (that's what virtual inheritance is for, basically, it's also called Diamond Inheritance). 他们的超类Interface将合并为一个Interface (这就是虚拟继承的用途,基本上,它也称为钻石继承)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM