C++ 从类内部访问成员变量的派生typeid

C++ 从类内部访问成员变量的派生typeid,c++,pointers,casting,polymorphism,C++,Pointers,Casting,Polymorphism,我正在制作一个捕食者对猎物的游戏,它有一个Being超类和Human/Zombie子类。它还有一个城市类,成员数组由生物组成,人类和僵尸最终将在其中移动 在驱动程序中,我能够使用以下代码输出城市类成员数组中的生物: for (int i = 0; i < 20; i++) { for (int j = 0; j < 20; j++) { if (city.getBeing(i, j) != NULL)

我正在制作一个捕食者对猎物的游戏,它有一个Being超类和Human/Zombie子类。它还有一个城市类,成员数组由生物组成,人类和僵尸最终将在其中移动

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

    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;
阵列存取器:

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;

}
#包括“City.h”
#包括“Human.h”
#包括“Zombie.h”
#包括“Being.h”
#包括
#包括
#包括
#包括
#包括
#包括
#包括
使用名称空间std;
类僵尸;
类人;
阶级存在;
无效输出();
城市我的城市;
人类;
存在&存在=人;
僵尸僵尸;
Being&being2=zomb;
整数x,y,num=0;
无效计时器\u启动(标准::函数func,无符号整数间隔)
{
线程([func,interval](){
while(true)
{
func();
std::this_线程::sleep_for(std::chrono::毫秒(间隔));
}
}).detach();
}
int main()
{
srand(时间(空));
while(num<10)
{
x=rand()%20+1;
y=rand()%20+1;
if(myCity.getBeing(x,y)==NULL)//确保不使用spot
{
我的城市。挫折(&being,x,y);
num+=1;
}
//////////
x=rand()%20+1;
y=rand()%20+1;
if(myCity.getBeing(x,y)==NULL)//确保不使用spot
{
我的城市。挫折(&being2,x,y);
num+=1;
}
}
定时器启动(输出,1000);
虽然(正确);
返回0;
}
void output()//每次勾选都会发生这种情况
{
//穿过阵列,移动每个人
myCity.move();

问:“
City::getBeing()
的定义是什么?它使用的是什么数据结构?“人类和僵尸最终将在其中移动的生物”,听起来很血腥。要动态检查类型,你可以更好地使用
dynamic\u cast
。但是,最好通过使用虚拟方法来避免强制转换。这就是它们的主要用途。@MilesBudnek添加代码是为了让你看得开心。接下来:
是一个多态类型吗?也就是说,它至少声明或继承了吗一个虚拟函数?如果不是,
typeid
将不会执行任何动态类型检查。
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;

}