1、typeid在没有虚函数时(不涉及到动态联编),typeid仅仅返回其操作对象的实际类型
2、typeid涉及到动态联编问题时(使用基类指针p或者引用p操作派生类对象),typeid(p)返回基类类型,typeid(*p)返回派生类类型;typeud(&p)返回基类类型,typeid(p)返回派生类类型
3、dynamic_cast在动态联编(实时类型信息)问题中,可以实现基类指针(或引用)和派生类指针(或引用)之间的尝试性动态转换
#include "stdafx.h"
#include <iostream>
#include <typeinfo>
using namespace std;
class Base
{
public:
int m_base;
virtual void show()
{
cout<<"This is Base class"<<endl;
}
};
class Derived:public Base
{
public:
int m_derived;
void show()
{
cout<<"This is Derived class"<<endl;
}
};
int main(int argc,char* argv[])
{
Base *pb=new Derived();
cout<<typeid(pb).name()<<endl;
cout<<typeid(*pb).name()<<endl;
system("pause");
return 0;
}