钻石继承(C++)

Diamond inheritance (C++)(钻石继承(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  {/*...*
                
本站部分内容来源互联网,如果有图片或者内容侵犯您的权益请联系我们删除!

相关文档推荐

Bring window to front -gt; raise(),show(),activateWindow() don’t work(把窗户放在前面 -raise(),show(),activateWindow() 不起作用)
How to get a list video capture devices NAMES (web cameras) using Qt (crossplatform)? (C++)(如何使用 Qt(跨平台)获取列表视频捕获设备名称(网络摄像机)?(C++))
How to compile Qt as static(如何将 Qt 编译为静态)
C++ over Qt : Controlling transparency of Labels and Buttons(C++ over Qt:控制标签和按钮的透明度)
How to know when a new USB storage device is connected in Qt?(Qt如何知道新的USB存储设备何时连接?)
What is an event loop in Qt?(Qt 中的事件循环是什么?)