C++:钻石问题

发布于 2024-12-26 10:03:51 字数 637 浏览 1 评论 0原文

简单的问题: 这种等级制度会导致DP吗?

//Abstract base class
class A {
    //Implement pure virtual methods with a default definition.
    //Pure virtual methods are still declared pure virtual.
};

//Abstract derived class
class B : public A {
    //Define pure virtual methods with a default definition.
    //Pure virtual methods are still declared pure virtual.
};

//Abstract base class
class C {
    //Define pure virtual methods with a default definition.
    //Pure virtual methods are still declared pure virtual.
};

//Concrete class
class D : public B, public C {
    //Implement all methods from D, B, and C
};

Simple question:
Will this hierarchy cause a DP?

//Abstract base class
class A {
    //Implement pure virtual methods with a default definition.
    //Pure virtual methods are still declared pure virtual.
};

//Abstract derived class
class B : public A {
    //Define pure virtual methods with a default definition.
    //Pure virtual methods are still declared pure virtual.
};

//Abstract base class
class C {
    //Define pure virtual methods with a default definition.
    //Pure virtual methods are still declared pure virtual.
};

//Concrete class
class D : public B, public C {
    //Implement all methods from D, B, and C
};

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

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

发布评论

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

评论(1

风苍溪 2025-01-02 10:03:51

不,画图;所有基数都是唯一的:

A
|
V
B   C
 \ /
  V
  D

“钻石问题”仅涉及由于多个重复基数而导致的不明确基类,例如,如果 D 是从 A -> A 派生而来的。 BA -> C:

No-diamond-problematic:                 Virtually-inherited diamond:

        A   A                                         A
        |   |                                       v/ \v
        B   C                                       B   C
         \ /                                         \ /
          V                                           V
          D                                           D

No. Draw the picture; all bases are unique:

A
|
V
B   C
 \ /
  V
  D

The "diamond problem" only pertains to ambiguous base classes due to multiple repeated bases, e.g. if D were to derive from both A -> B and A -> C:

No-diamond-problematic:                 Virtually-inherited diamond:

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