C++多重继承阻止钻石

发布于 2024-08-22 18:24:35 字数 261 浏览 6 评论 0原文

有没有办法在 C++ 中定义类 Foo 这样

  1. 我就可以继承它,
  2. 我不能从中“钻石继承”,

class Cat: public Foo{} // okay
class Dog: public Foo{} // okay
class Weird: public Cat, public Dog {} // I want this to throw a compiler error

Is there a way to define a class Foo in C++
so that

  1. I can inherit from it
  2. I can't "diamond inherit" from it

I.e.

class Cat: public Foo{} // okay
class Dog: public Foo{} // okay
class Weird: public Cat, public Dog {} // I want this to throw a compiler error

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

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

发布评论

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

评论(2

千里故人稀 2024-08-29 18:24:35

Cprogramming.com 教程:用 ... 解决 C++ 中的钻石问题

http://www.cprogramming .com/tutorial/virtual_inheritance.html

尝试一下

对于这种类型的问题可以通过接口来避免或解决。

Cprogramming.com Tutorial: Solving the Diamond Problem in C++ with ...

http://www.cprogramming.com/tutorial/virtual_inheritance.html

Try This

For this type problem can be Avoid or solve by interface.

吾性傲以野 2024-08-29 18:24:35

另一个信息来源:

http://www.parashift.com/c++ -faq-lite/multiple-inheritance.html

实际上,整个 C++ 常见问题解答很少,如果您正在使用 C++ 进行编程,那么它确实值得一读。

Another source of information:

http://www.parashift.com/c++-faq-lite/multiple-inheritance.html

Actually the whole C++ faq little it is really worth reading if you are programming on C++.

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