C ++生态系统模拟器(继承) [英] C++ ecosystem simulator (inheritance)

查看:282
本文介绍了C ++生态系统模拟器(继承)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是使用C ++开发生态系统的初学者.它是这样的:

I am a beginner developing an ecosystem in C++. It works like this:

我的网格上有PLANTS(此处未显示)和ANIMALS.

I have PLANTS(not shown here) and ANIMALS on my grid.

如果某物是ANIMAL,则它是CARNIVOREHERBIVORE.

If something is an ANIMAL, it is either a CARNIVORE or an HERBIVORE.

我创建了此层次结构:

class ANIMAL
{
private:
    int a_CUR_food_lvl; 
    const int a_MAX_food_lvl; 
    const int a_hunger_rate; 

public:

    ANIMAL(int curr, int max, int rate)
        : a_CUR_food_lvl(curr),
        a_MAX_food_lvl(max),
        a_hunger_rate(rate)
    {}

     virtual ~ANIMAL()
     {
        delete this;
     }

    virtual void Move() = 0;
    virtual void Eat() = 0;
    virtual void Evade() = 0;
    virtual void Reproduce() = 0;
    virtual void Die() = 0;
    virtual void Age() = 0;

    virtual int get_a_CUR_food_lvl() { return a_CUR_food_lvl; };
    virtual int get_a_MAX_food_lvl() { return a_MAX_food_lvl; };
    virtual int get_a_hunger_rate() { return a_hunger_rate; };
}; //end ANIMAL class


//#################################################################
class CARNIVORE : public ANIMAL
{
public:

    class WOLF : public ANIMAL
    {
        WOLF()
            : ANIMAL(150, 200, 2)
        {}
    };

    class CHEETAH : public ANIMAL
    {
        CHEETAH()
            : ANIMAL(75, 125, 5)
        {}
    };
}; //end CARNIVORE class


//#################################################################
class HERBIVORE : public ANIMAL
{
public:


    class RABBIT : public ANIMAL
    {
        RABBIT()
            : ANIMAL(150, 200, 2)
        {}
    };


    class SQUIRREL : public ANIMAL
    {
        SQUIRREL()
            : ANIMAL(150, 200, 2)
        {}
    };
}; //end HERBIVORE class

我的问题:

稍后,我想使用 dynamic_cast 来查看是否允许一只动物吃另一只动物.具体来说,仅当ACARNIVOREBHERBIVORE时,才允许Animal A吃动物B.

Later down the line, I want to use dynamic_cast to see if one animal is allowed to eat another animal. Specifically, Animal A is only allowed to eat animal B if A is a CARNIVORE and B an HERBIVORE.

是否会像我以前那样嵌套类,以便稍后在编程时检查对象的类型?我不想现在认为这是正确的,而必须将其更改为下一行.

Will nesting the classes like I did allow me to check the type of the object later in my programming? I wouldn't want to think this is correct now and have to change it down the line.

或者如果除了dynamic_cast(布尔值is_carnivore?)之外还有更好的方法,我应该使用它吗?我的目标是最易读和最佳的OOP做法

Or if there is a better way other than dynamic_cast (a boolean is_carnivore?) should I be using that instead? I'm aiming for the most readable and best OOP practice

推荐答案

您的嵌套类根本不代表您想要的.不要再考虑使用此结构来解决您的问题.相反:

Your nested classes do not at all represent what you want. Do not consider further this construct to address your problem. Instead:

  • WOLF和CHEETAH应该从CARNIVORE继承
  • SQUIRREL和兔子应从HERBIVORE继承

然后有几种方法可以查看动物*a是否可以吃另一只动物*b.如您所建议,最简单的方法是dynamic_cast:

There are then several ways to see if animal *a can eat animal *b another. The simplest approach would be dynamic_cast as you've suggested:

ANIMAL *a, *b; 
...
if (dynamic_cast<CARNIVORE*>(a) && dynamic_cast<HERBIVORE*>(b)) {
     // *a can eat *b
}

但这不是最佳的OOP做法.我不知道您的所有限制和要求,但是如果您想成为最先进的技术并能够编码更复杂的规则(例如主角的力量和健康状况,某些物种的例外情况等),您可能会有兴趣实施某种双重分发(例如讨论过的

But this would not be the best OOP practice. I don't know all your constraints and requirements, but if you want to be state of the art and be able to encode much more complex rules (like strength and health of protagonists, exceptions for some species, etc..) you may be interested in implementing some kind of double dispatch (such as discussed here

这篇关于C ++生态系统模拟器(继承)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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