简体   繁体   English

c++:多重继承和接口

[英]c++: Multiple inheritance and interface

See following code:请参见以下代码:

#include <iostream>

struct A {  // Interface
    virtual void a() = 0;
    virtual void x() = 0;
};

struct B {
    virtual void a() { std::cout << "B" << std::endl; }
};

struct C : A, B {
    void a() override { B::a(); }
    void x() override { std::cout << "x" << std::endl; }
};

int main() {
    C c;
    c.a();
    c.x();
}

This code works.此代码有效。 The question is if it is the best/optimal solution.问题是它是否是最佳/最佳解决方案。

I wonder if there is any trick which allow me not to create a() in C class.我想知道是否有任何技巧可以让我不在 C 类中创建 a() 。

Update: I corrected the code to show why B cannot inherit from A.更新:我更正了代码以说明为什么 B 不能从 A 继承。

Update 2:更新 2:

#include <iostream>
    
struct I1 {
    virtual void i1() = 0;
};

struct I2 {
    virtual void i2() = 0;
};

struct I3 : I1, I2 {};

struct A : I1 {
    void i1() override { std::cout << "i1" << std::endl; }
};

struct B : A, I2 {
    void i2() override { std::cout << "i2" << std::endl; }
};

int main() {
    B b;
    b.i1();
    b.i2();

    I3* ptr = dynamic_cast<I3*>(&b);  // warning: dynamic_cast of ‘B b’ to ‘struct I3*’ can never succeed
    std::cout << ptr << std::endl;
}

The question is: How to pass pointer to 'b' via interface?问题是:如何通过接口将指针传递给“b”? Why b cannot be casted to I3 interface?为什么 b 不能转换为 I3 接口?

You can consider to split A into 2 interfaces:您可以考虑将A拆分为 2 个接口:

struct A1 {  // Interface1
    virtual void a() = 0;
    virtual ~A1() {}
};

struct A2 {  // Interface2
    virtual void x() = 0;
    virtual ~A2() {}
};

This way you can:这样您就可以:

  1. Make B inherit from A1 and implement a() .使BA1继承并实现a()
  2. Make C inherit from A2 (to implement x() ) and from B to get the implementation for a() .使C从 A2 继承(以实现x() )并从B继承以获取a()的实现。

See below:见下文:

#include <iostream>

struct B : public A1 {
    virtual void a() override { std::cout << "B" << std::endl; }
};

struct C : public A2, public B {
    virtual void x() override { std::cout << "x" << std::endl; }
};

int main() {
    C c;
    c.a();
    c.x();
}

Note: I added virtual destructors to the base classes.注意:我在基类中添加了虚拟析构函数。 See here why: When to use virtual destructors?看这里为什么: 什么时候使用虚拟析构函数? . .

Depending on what you are trying to achieve you could use classes and make the function in B private so no one can try and use it.根据您要实现的目标,您可以使用类并将 B 中的函数设为私有,因此没有人可以尝试使用它。 Something like:就像是:

class A {  // Interface
public:
    virtual ~A() {}

    virtual void a() = 0;
    virtual void x() = 0;
};

class B : public A{
private:
    void x() { std::exception("Not Implemented"); };

public:
    ~B() override{}
    void a() override { std::cout << "B" << std::endl; }
};

class C : public B {
public:
    ~C() override{}
    void x() override { std::cout << "x" << std::endl; }
};

Other possible solutions are:其他可能的解决方案是:

  • Have B as a member of C, so you can call Ba() rather than multiple inheritance to make it a bit less confusing for readers将 B 作为 C 的成员,因此您可以调用 Ba() 而不是多重继承,以减少对读者的混淆
struct A {  // Interface
    virtual ~A() {}
    virtual void a() = 0;
    virtual void x() = 0;
};

struct B {
    virtual void a() { std::cout << "B" << std::endl; }
};

struct C : A {
    B b;

    virtual ~C() override {}
    void a() override { b.a(); }
    void x() override { std::cout << "x" << std::endl; }
};

int main() {
    C c;
    c.a();
    c.x();
}

You could use function pointers - but this means the functions must be static, and your base class becomes an abstract class您可以使用函数指针 - 但这意味着函数必须是静态的,并且您的基类成为抽象类

struct A {  // Interface
    virtual ~A() {}

    
    virtual void x() = 0;
    typedef void(*aFunction)();
    aFunction a;

    A() { a = nullptr; }
   
};

struct B {
    static void a() { std::cout << "B" << std::endl; }
};

struct C : A {
    virtual ~C() {}

    void x() override { std::cout << "x" << std::endl; }
    C() { a = B::a; }
};

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

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