C++中实现多态时怎样判断一个父类指针指向哪一个子类对象?
C++中实现多态时怎样判断一个父类指针指向哪一个子类对象?就像Java中Instanceof()方法,我用typeid做不出来
#include "Person.h"
#include "Student.h"
#include <iostream>
#include <typeinfo>
using namespace std;
int main()
{
Person* p = new Student(1, "小李", MALE, 21, 3, 1);
p->print();
if(typeid(p) == typeid(Student))
{
cout << "Yes" << endl;
}
else
{
cout << "No" << endl;
cout << typeid(new Student(1, "小李", MALE, 21, 3, 1)).name() << endl;
}
delete p;
return 0;
}