简体   繁体   English

在C ++中,“具有”关系是否是使一个类“实现”多个抽象基类的最佳实践?

[英]“Has a” relationships in C++, is it best practice to have a class “implement” multiple abstract base classes?

In Java, you often use interfaces to indicate that a class will have a particular function. 在Java中,您经常使用接口来指示类将具有特定功能。

In c++ I know this is done by abstract classes, however, I know there is often a stigma that goes a long with the multiple inheritance paradigm. 在c ++中,我知道这是由抽象类完成的,但是,我知道多重继承范例经常会给人留下很长的烙印。

For example, Imagine you have game objects that will need to have functions on them: extends DealDamage will have a DoDamage() function extends TakeDamage will have a TakeDamage() function etc. How would you best handle this in C++? 例如,假设您有一些游戏对象需要在其上具有功能:扩展DealDamage将具有DoDamage()函数扩展TakeDamage将具有TakeDamage()函数等。如何在C ++中最好地处理此问题?

If all you want is a contract-based ("interface, extends") design approach, just create classes that define the interfaces your implementation class is guaranteed to support. 如果您想要的只是一种基于合同的(“接口,扩展”)设计方法,则只需创建定义您的实现类可以保证支持的接口的类即可。

For example: 例如:

class ITakeDamage {
    virtual ~ITakeDamage() {}     // virtual dtors for this design!
    virtual void TakeDamage(const DamageMeasurement& hurtFactor) = 0; // ouch!
    // ... some more stuff
};

class IDoDamage {
public:
    virtual ~IDoDamage() {}
    virtual void HurtSomething(ITakeDamage* recipient, DamageMeasurement hurtFactor) = 0; // take that!
    // ... some more stuff
};

class NormalThingThatTakesAndGivesDamage : public ITakeDamage, public IDoDamage {
public:
    NormalThingThatTakesAndGivesDamage();
    ~NormalThingThatTakesAndGivesDamage();
    // etc...

    // ITakeDamage interface members...
    virtual void TakeDamage(const DamageMeasurement& hurtFactor) override;

    // IDoDamage interface members...
    virtual void HurtSomething(ITakeDamage* recipient, DamageMeasurement hurtFactor) override; // implement this
};


class IndestructibleThingThatHurtsOthers : public IDoDamage {
public:
    IndestructibleThingThatHurtsOthers();
    ~IndestructibleThingThatHurtsOthers();
    // etc...

    // IDoDamage interface members...
    virtual void HurtSomething(ITakeDamage* recipient, DamageMeasurement hurtFactor) override; // implement this
};

class HarmlessTarget : public ITakeDamage {
public:
    HarmlessTarget();
    ~HarmlessTarget();
    // etc...

    // ITakeDamage interface members...
    virtual void TakeDamage(const DamageMeasurement& hurtFactor) override; // implement this
};

As Sam said in his comment, multiple inheritance is one of the more powerful features available in C++. 正如Sam在评论中所说,多重继承是C ++中可用的更强大的功能之一。 Although I've used pure virtual interfaces here as an example (based on the "interface" portion of your question), inheriting from multiple base classes that also provide implementations is also appropriate in some cases. 尽管我在这里使用了纯虚拟接口作为示例(基于问题的“接口”部分),但在某些情况下,从多个也提供实现的基类继承也是合适的。 Used judiciously and with the right design, it can even considerably simplify relationships between classes. 明智地使用并采用正确的设计,它甚至可以大大简化类之间的关系。 Used poorly, it can create a complete mess. 使用不当,会造成完全混乱。 Any "stigma" is generally from limited experience confined to bad or improper usage. 通常,任何“污名”都来自有限的经验,仅限于不良或不当使用。

Just because a tool isn't the right one for every situation, don't take it out of your toolbox. 仅仅因为一种工具在每种情况下都不适合,所以不要将其从工具箱中取出。 IMHO, if it makes your architecture simpler, more robust and easier to maintain, go for it. 恕我直言,如果它使您的体系结构更简单,更健壮并且更易于维护,那就去吧。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 难以实现类型抽象基 class 的 C++ 数组,同时其中有多个不同的派生类 - Difficulty implement a C++ array of type abstract base class while having multiple different derived classes in it 在C ++单元测试上下文中,抽象基类是否应该将其他抽象基类作为函数参数? - In a C++ unit test context, should an abstract base class have other abstract base classes as function parameters? 在 C++ 中实现类似接口的纯抽象基类的最佳实践? - Best practice for implementing an interface-like pure abstract base class in C++? 在C ++中设计ABC(抽象基类)的好习惯 - Good practice to design a ABC(Abstract Base Class) in C++ 从基类中“导入”函数的定义以实现抽象接口(C ++中的多重继承) - “import” a definition of a function from a base class to implement abstract interface (multiple inheritance in C++) 抽象基类c ++ - Abstract base class c++ C++ 抽象基 class - C++ abstract base class 关于C ++中抽象基类的问题 - Question on abstract base classes in c++ C ++抽象基类的集合 - C++ collection of abstract base classes 抽象类有什么需要? 为什么要通过其基类访问派生类方法? 在C ++中 - What is the need of abstract classes ? Why access a derived classes method through its base class ? in C++
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM