Diamond inheritance (C++)(钻石继承(C++))
问题描述
我知道拥有钻石继承被认为是不好的做法.但是,我有两个案例,我觉得钻石继承非常适合.我想问一下,在这些情况下你会推荐我使用菱形继承,还是有其他更好的设计.
I know that having diamond inheritance is considered bad practice. However, I have 2 cases in which I feel that diamond inheritance could fit very nicely. I want to ask, would you recommend me to use diamond inheritance in these cases, or is there another design that could be better.
案例 1: 我想在我的系统中创建代表不同类型操作"的类.动作由几个参数分类:
Case 1: I want to create classes that represent different kinds of "Actions" in my system. The actions are classified by several parameters:
- 操作可以是读取"或写入".
- 动作可以有延迟或没有延迟(它不仅仅是 1 个参数.它会显着改变行为).
- 动作的流类型"可以是 FlowA 或 FlowB.
我打算有以下设计:
// abstract classes
class Action
{
// methods relevant for all actions
};
class ActionRead : public virtual Action
{
// methods related to reading
};
class ActionWrite : public virtual Action
{
// methods related to writing
};
class ActionWithDelay : public virtual Action
{
// methods related to delay definition and handling
};
class ActionNoDelay : public virtual Action {/*...*
本文标题为:钻石继承(C++)


基础教程推荐
- Windows Media Foundation 录制音频 2021-01-01
- 在 C++ 中循环遍历所有 Lua 全局变量 2021-01-01
- 为 C/C++ 中的项目的 makefile 生成依赖项 2022-01-01
- 管理共享内存应该分配多少内存?(助推) 2022-12-07
- 如何在不破坏 vtbl 的情况下做相当于 memset(this, ...) 的操作? 2022-01-01
- 如何“在 Finder 中显示"或“在资源管理器中显 2021-01-01
- 为什么语句不能出现在命名空间范围内? 2021-01-01
- 使用从字符串中提取的参数调用函数 2022-01-01
- 从 std::cin 读取密码 2021-01-01
- 如何使图像调整大小以在 Qt 中缩放? 2021-01-01