从类内部访问成员var的派生typeid

时间:2015-12-04 01:33:46

标签: c++ pointers casting polymorphism

我正在制作捕食者与猎物游戏,它有一个存在超类和人类 / 僵尸子类。它还有一个 City 类,其成员阵列由人类和僵尸最终将在其中移动而成。

在驱动程序中,我能够使用以下代码在类城市的成员数组中输出生物:

    for (int i = 0; i < 20; i++)
    {
        for (int j = 0; j < 20; j++)
        {
            if (city.getBeing(i, j) != NULL)
            {

                if (typeid(Human).name() == typeid(*city.getBeing(i, j)).name())
                {
                    output << "H ";
                }

                else if (typeid(Zombie).name() == typeid(*city.getBeing(i, j)).name())
                {
                    output << "Z ";
                }
                else
                {
                    output << "_ ";
                }
            }
            else
            {
                output << "- ";
            }
        }
        output << endl;
    }
    return output;

现在我已将此代码移至&lt;&lt; City 类中的ostream 重载...

ostream& operator<<(ostream &output, City &city)
{
...
}

getBeing()仅返回Being typeids。我如何解决这个问题,以便返回适当的人/僵尸类型?使用动态/静态演员会更好吗?

编辑:

城市成员阵列:

Being *grid[GRID_HEIGHT][GRID_WIDTH];//holds beings

数组访问者:

Being *City::getBeing(int x, int y)
{
    return grid[x][y];
}

全程驱动程序和城市类代码:

#include "City.h"
#include "Human.h"
#include "Zombie.h"
#include "Being.h"

#include <conio.h>
#include <time.h>
#include <typeinfo> 
#include <iostream>
#include <chrono>
#include <thread>
#include <functional>

using namespace std;

class Zombie;
class Human;
class Being;
void output();

City myCity;

Human human;
Being& being = human;
Zombie zomb;
Being& being2 = zomb;

int x, y, num = 0;



void timer_start(std::function<void(void)> func, unsigned int interval)
{
    std::thread([func, interval]() {
        while (true)
        {
            func();
            std::this_thread::sleep_for(std::chrono::milliseconds(interval));
        }
    }).detach();
}

int main()
{

    srand(time(NULL));
    while (num < 10)
    {

        x = rand() % 20 + 1;
        y = rand() % 20 + 1;

        if (myCity.getBeing(x, y) == NULL)//makes sure spot isn't taken
        {
            myCity.setBeing(&being, x, y);
            num += 1;
        }
        //////////
        x = rand() % 20 + 1;
        y = rand() % 20 + 1;

        if (myCity.getBeing(x, y) == NULL)//makes sure spot isn't taken
        {
            myCity.setBeing(&being2, x, y);
            num += 1;
        }


    }


    timer_start(output, 1000);

    while (true);

    return 0;
}



void output()//this happens each tick
{


//go through array and move each guy
    myCity.move();

    cout << myCity << endl << endl;

}
#include "City.h"
#include "Human.h"
#include "Zombie.h"
#include "Being.h"

#include <conio.h>
#include <time.h>
#include <typeinfo> 
#include <iostream>
#include <chrono>
#include <thread>
#include <functional>

using namespace std;

class Zombie;
class Human;
class Being;
void output();

City myCity;

Human human;
Being& being = human;
Zombie zomb;
Being& being2 = zomb;

int x, y, num = 0;



void timer_start(std::function<void(void)> func, unsigned int interval)
{
    std::thread([func, interval]() {
        while (true)
        {
            func();
            std::this_thread::sleep_for(std::chrono::milliseconds(interval));
        }
    }).detach();
}

int main()
{

    srand(time(NULL));
    while (num < 10)
    {

        x = rand() % 20 + 1;
        y = rand() % 20 + 1;

        if (myCity.getBeing(x, y) == NULL)//makes sure spot isn't taken
        {
            myCity.setBeing(&being, x, y);
            num += 1;
        }
        //////////
        x = rand() % 20 + 1;
        y = rand() % 20 + 1;

        if (myCity.getBeing(x, y) == NULL)//makes sure spot isn't taken
        {
            myCity.setBeing(&being2, x, y);
            num += 1;
        }


    }


    timer_start(output, 1000);

    while (true);

    return 0;
}



void output()//this happens each tick
{


//go through array and move each guy
    myCity.move();

    cout << myCity << endl << endl;

}

0 个答案:

没有答案