避免在C ++中重复的子类定义 [英] Avoiding repetitive sub-class definitions in C++

查看:57
本文介绍了避免在C ++中重复的子类定义的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是C ++类的新手,我对定义具有相同定义的抽象类型/接口的多个子类有疑问.

I am new to C++ classes, and I have a question about defining multiple sub-classes of an abstract type/interface which would have identical definitions.

以下面的示例为例,该示例可能会出现在具有3个子类的头文件中:

Take the following example which might appear in a header file with 3 sub-classes:

class Animal {
private:
    int a;
    int b;
public:
    explicit Animal(int a) {}
    virtual Animal* getFriend() = 0;
    virtual bool walk() = 0;
    virtual bool talk() = 0;
    virtual bool someFunction() = 0;
    virtual bool someOtherFunction() = 0;
    // ... many more functions
}

class Zebra: public Animal {
    Animal* getFriend();
    bool walk();
    bool someFunction();
    bool someOtherFunction();
    // ... continues for many more functions
}

class Cow: public Animal {
    Animal* getFriend();
    bool walk();
    bool someFunction();
    bool someOtherFunction();
    // ... continues for many more functions
}

class Salmon: public Animal {
    Animal* getFriend();
    bool walk();
    bool someFunction();
    bool someOtherFunction();
    // ... continues for many more functions
}

// ... many more animals

像这样声明子类似乎是重复的,并且容易出错.因为除了类名之外,这些类的定义是相同的,所以有一种更有效的方法来批量声明动物子类吗?

Declaring the sub-classes like this seems repetitive and potentially error prone. Because the class definitions are identical except for the name of the class, is there a more efficient way to declare the animal sub-classes in bulk?

在上下文中,我在每种动物中工作的单独的 .cpp 文件中将具有完全独立的实现.

In the context I am working in each animal would have a completely independent implementation in separate .cpp files.

请让我知道我是否正在完全犯错.任何帮助将不胜感激.

Please let me know if i'm approaching this completely wrong. Any help would be greatly appreciated.

推荐答案

使用宏定义类的简短操作(这甚至更糟!),您可能无能为力.有时候这样的事情可能会起作用,但我想在某个时候打赌,您想对其中一种动物进行专门化处理,导致您再次放弃该宏.出于这个原因,我会避免使用该特定技术.

Short of using a macro to define the classes (which is even worse!), there probably isn't a great deal you can do. Occasionally stuff like this might work, but I'd wager that at some point, you'd want to specialise one of the animals, leading to you ditching the macro again. It's for that reason I'd avoid that particular technique.

#define DECLARE_ANIMAL(ANIMAL_TYPE) \
class ANIMAL_TYPE: public Animal { \
    Animal* getFriend() override; \
    bool walk() override; \
    bool someFunction() override; \
    bool someOtherFunction() override; \
};
DECLARE_ANIMAL(Zebra);
DECLARE_ANIMAL(Cow);
DECLARE_ANIMAL(Salmon);

通常来说,请尝试移动尽可能多的重复的类方法&将数据放入基类中,以最大程度地减少代码重复量.但这可能需要您对问题的思考方式稍作更改....

Generally speaking, try to move as much of the duplicated class methods & data into the base class to minimise the amount of code duplication. This may require a slight change in the way you think about the problem though....

例如,walk().在母牛/斑马/马/猫/狗的情况下,走路的动作几乎是相同的.唯一的实际差异可以用数据(例如,步行速度,使用了多少条腿,步行的步态是多少,每个步幅有多大?)来衡量.如果可以以数据驱动的方式定义行为,则只需要在Derived类构造函数中设置这些参数即可,而无需定制方法.以这种方式进行班级设计还有其他一些好处,例如您只有一个狗"班级,但无需创建新班级就可以代表4条腿的狗和3条腿的狗..

For example, walk(). In the case of a Cow/Zebra/Horse/Cat/Dog, the act of walking is pretty much identical. The only real differences can be measured with data (e.g. the walk speed, how many legs are used, what is the gait of the walk, how large is each stride?). If you can define the behaviour in a data-driven fashion, you would just need to set those parameters in the Derived class constructor, and avoid the need for customised methods. Approaching the class design this way has a few other benefits, for example you'd have a single 'Dog' class, but it would be able to represent a 4 legged dog, and a 3 legged dog, without needing to create a new class.

无论如何,这通常是我推荐的方法...

That's usually the approach I'd recommend anyway...

这篇关于避免在C ++中重复的子类定义的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆