在“final”上使用虚拟继承未完成的类层次结构中的类

发布于 2024-11-10 17:58:34 字数 109 浏览 5 评论 0原文

在未完成的类层次结构中抢先派生当前处于“底部”(即最派生的)的虚拟类是否有任何危害,或者是否被认为是糟糕的设计?是否有充分的理由让人们愿意等到真正需要虚拟继承时(即,当有人决定扩展层次结构并产生钻石时)?

Is there any harm or is it considered bad design to preemptively derive virtually classes in an unfinished class hierarchy that are currently "at the bottom" (i.e., the most derived)? Is there a good reason one would want to wait until there was actually a need for virtual inheritance (i.e., when someone decides to extend the hierarchy and it results in a diamond)?

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

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

发布评论

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

评论(2

冬天旳寂寞 2024-11-17 17:58:34

在实际需要之前我会避免虚拟继承。当您使用虚拟继承时,您会泄漏在类上构建的部分抽象,特别是通过强制对最派生类型调用虚拟基类来初始化基类。

I would avoid virtual inheritance until actually needed. When you use virtual inheritance you are leaking part of the abstractions that you build on your class, and in particular how you initialize your base class, by forcing the call to the virtual base to the most derived type.

听风念你 2024-11-17 17:58:34

它增加了不需要它的系统的复杂性。

当您根据未来的需求向代码库添加功能或特性时,您通常会错过目标。有时,确实有一天会需要该功能 - 即使不是今天 - 但在许多情况下不会。结果是大型、复杂的类更难以理解,更难以调试、维护和扩展,并且可能在效率方面花费更多。

KISS 原则的存在是有原因的。它可以防止您的代码成为 Rube Goldberg 机器

It adds complexity to a system that doesn't need it.

When you add functionality or features to a codebase in anticipation of future needs, you will often miss the target. Sometimes that functionality will, indeed, be needed some day -- even if not today -- but in many cases it will not. What results is large, complicated classes that are more difficult to understand, more difficult to debug, maintain and extend, and may cost more in terms of efficiency.

The KISS principle exists for a reason. It keeps your code from becoming a Rube Goldberg machine.

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