简体   繁体   English

都继承了相同类的不同对象的单个容器

[英]Single container of different objects that all inherited the same class

I'm trying to accomplish something but am unsure if it's even possible. 我正在尝试完成某事,但不确定是否有可能。

The quick idea is, I'm writing a game and want to have a single array (or vector) of different monsters. 一个简单的主意是,我正在编写一个游戏,并且希望有一个由不同怪物组成的数组(或向量)。 Every class that inherits the main class Monster simply overrides its functions (but doesn't add any new ones). 继承Monster主类的每个类都将简单地覆盖其功能(但不会添加任何新功能)。

Then, when I go through the list of monsters, I can just call the same functions that all of them have. 然后,当我浏览怪物列表时,我可以调用所有怪物都具有的相同功能。

Here's some code to show what I'm trying to accomplish: 这是一些代码来显示我要完成的工作:

class Monster
{
    public:
        int hp;  //hit points
        int atp; //attack power
        int def; //defense

        bool attacking;
        bool defending;

        virtual void attack();
        virtual void defend();
};

void Monster::attack()
{}

void Monster::defend()
{}



class Goblin: public Monster
{
    public:
        virtual void attack() override;
        virtual void defend() override;
};

void Goblin::attack()
{
    //Goblin's attacking patterns
}

void Goblin::defend()
{
    //Goblin's defending patterns
}


class Orc: public Monster
{
    public:
        virtual void attack() override;
        virtual void defend() override;
};

void Orc::attack()
{
    //Orc's attacking patterns
}

void Orc::defend()
{
    //Orc's defending patterns
}




int main(void)
{
    //This is where I'm not sure what to do:

    //Initialize monsters.  Make some Goblins, some Orcs
    int num_monsters = 10;
    Monster* monster_list;
    monster_list = new Monster[num_monsters];

    for (int i = 0; i < num_monsters; i++)
    {
        int which = rand() % 2;
        switch (which)
        {
            case 0:  //Goblin
               monster_list[i] = new Goblin;                   
               break;
            case 1:  //Orc
               monster_list[i] = new Orc;                   
               break;
        }
    }

    bool quit = false;
    while (quit == false)
    {
        for (int i = 0; i < num_monsters; i++)
        {
            if (monster_list[i].attacking == true)
                monster_list[i].attack();
            if (monster_list[i].defending == true)
                monster_list[i].defend();                
        }
    }
}

Hopefully that illustrates what I'm trying to do. 希望这可以说明我正在尝试做的事情。 I know this doesn't work, but I'm not sure how to make it work. 我知道这是行不通的,但是我不确定如何使它生效。 Thanks for any advice on this! 感谢您对此的任何建议!

You'll need to use a vector of pointers to a base class. 您将需要使用指向基类的指针向量。

std::vector<Monster*> monsters;

monsters.push_back(new FireDragon());
monsters.push_back(new IceDragon());

Then you'll be able to iterate through the monsters vector and call a common method. 然后,您将能够遍历Monsters向量并调用通用方法。

for(auto monster = monsters.begin(); monster != monsters.end(); monster++)
{
    (*monster)->attack();
}

The classes: 这些课程:

class Monster {
public:
    virtual ~Monster() {}
    virtual void attack() = 0;
};

class FireDragon : public Monster {
public:
    ~FireDragon();
    void attack()
    {
        std::cout << "Fire breath!" << std::endl;
    }
};

class IceDragon : public Monster {
public:
    ~IceDragon();
    void attack()
    {
        std::cout << "Ice breath!" << std::endl;
    }
};

As a side note be sure to create virtual destructors in the derived classes or else the base class' destructor will be called. 另外,请确保在派生类中创建虚拟析构函数,否则将调用基类的析构函数。

ETA: Here is the implementation with smart pointers: ETA:这是带有智能指针的实现:

/*
Use std::unique_ptr<Monster> if your implementation doesn't need to pass the
monster objects around
*/

std::vector<std::shared_ptr<Monster>> monsters;

/*
Use std::make_unique<FireDragon>() if using unique_ptr
*/

monsters.push_back(std::make_shared<FireDragon>());
monsters.push_back(std::make_shared<IceDragon>());

for(auto monster : monsters)
{
    monster->attack();
}

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

相关问题 将具有继承同一类的不同类型的对象存储在容器中 - Store objects with different types that inherit the same class in a container 使用向量存储从同一个父类继承的不同对象C ++ - Using vectors to store different objects inherited from the same parent class c++ 将不同的模板化类存储在同一容器中 - store different templated class in the same container 如何创建一个动态数组,该数组可以容纳所有不同的对象,这些对象都源自C ++中的同一基类 - How to create a dynamic array that can hold different objects all derived from the same base class in C++ 在编译时创建具有不同功能的相同类的对象 - Creating Objects of same Class with different Functions at Compiletime 为同一类的两个对象绘制不同的图像 - Drawing a different image for two objects of the same class 用继承的类覆盖相同的函数 - Override of the same function with inherited class 为什么所有对象的类的静态成员都相同? - Why are the static members of a class the same for all objects? 是同一个memory分配给class的所有对象 - Is the same memory allocated to all objects of class 在同一个向量中存储两个不同的类(具有相同的继承基类)? (没有提升) - store two different classes (with same inherited base class) in same vector? (no boost)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM