C++多重继承 - 为什么你不工作?

发布于 2024-11-04 20:24:15 字数 716 浏览 0 评论 0原文

我正在尝试找出一个有趣的多重继承问题。

祖父母是一个具有多个方法的接口类:

class A
{
public:
    virtual int foo() = 0;
    virtual int bar() = 0;
};

然后是部分完成该接口的抽象类。

class B : public A
{
public:
    int foo() { return 0;}
};

class C : public A
{
public:
    int bar() { return 1;}
};

我想要使​​用的类继承自两个父类,并通过 using 指令指定应该来自何处的方法:

class D : public B, public C
{
public:
    using B::foo;
    using C::bar;
};

当我尝试实例化 DI 时,会因尝试实例化抽象类而出现错误。

int main()
{
    D d; //<-- Error cannot instantiate abstract class.

    int test = d.foo();
    int test2 = d.bar();

    return 0;
}

有人可以帮助我理解这个问题以及如何最好地利用部分实现吗?

I am trying to figure out an interesting multiple inheritance issue.

The grandparent is an interface class with multiple methods:

class A
{
public:
    virtual int foo() = 0;
    virtual int bar() = 0;
};

Then there are abstract classes that are partially completing this interface.

class B : public A
{
public:
    int foo() { return 0;}
};

class C : public A
{
public:
    int bar() { return 1;}
};

The class I want to use inherits from both of the parents and specifies what method should come from where via using directives:

class D : public B, public C
{
public:
    using B::foo;
    using C::bar;
};

When I try to instantiate a D I get errors for trying to instantiate an abstract class.

int main()
{
    D d; //<-- Error cannot instantiate abstract class.

    int test = d.foo();
    int test2 = d.bar();

    return 0;
}

Can someone help me understand the problem and how to best make use of partial implementations?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

小女人ら 2024-11-11 20:24:15

你没有钻石继承权。 DBC 基类各有自己的 A 基类子对象,因为它们不是虚拟继承的来自A

因此,在 D 中,实际上需要实现四个纯虚成员函数:A::fooA::bar来自 B 以及来自 CA::fooA::bar

您可能想使用虚拟继承。类声明和基类列表如下所示:

class A
class B : public virtual A
class C : public virtual A
class D : public B, public C

如果您不想使用虚拟继承,那么您需要重写 D 中的其他两个纯虚函数:

class D : public B, public C
{
public:
    using B::foo;
    using C::bar;

    int B::bar() { return 0; }
    int C::foo() { return 0; }
};

You don't have diamond inheritance. The B and C base classes of D each have their own A base class subobject because they do not inherit virtually from A.

So, in D, there are really four pure virtual member functions that need to be implemented: the A::foo and A::bar from B and the A::foo and A::bar from C.

You probably want to use virtual inheritance. The class declarations and base class lists would look like so:

class A
class B : public virtual A
class C : public virtual A
class D : public B, public C

If you don't want to use virtual inheritance then you need to override the other two pure virtual functions in D:

class D : public B, public C
{
public:
    using B::foo;
    using C::bar;

    int B::bar() { return 0; }
    int C::foo() { return 0; }
};
执着的年纪 2024-11-11 20:24:15

您需要将基类设为虚拟,以便它们能够正确继承。一般规则是所有非私有成员函数和基类都应该是虚拟的,除非您知道自己在做什么并且想要禁用该成员/基类的正常继承。

You need to make your base classes virtual in order for them to inherit properly. The general rule is that all non-private member functions and base classes should be virtual UNLESS you know what you're doing and want to disable normal inheritance for that member/base.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文