//typeid
//用法待查阅手册
#include <iostream>
#include <typeinfo>
using namespace std;
class A
{
public: virtual void show() {}
};
class B : public A{};
class C : public A{};
class D : public B{};
void type(A* pa)
{
if(typeid(*pa) == typeid(B))
{
cout << "B" << endl;
}
else if(typeid(*pa) == typeid(C))
{
cout << "B" << endl;
}
else if(typeid(*pa) == typeid(D))
{
cout << "D" << endl;
}
}
int main()
{
A* pa = new B;
type(pa);
A* pb = new D;
type(pb);
}
/* 程序运行结果
B
D
*/
typeid 相关资料:http://www.cppblog.com/smagle/archive/2010/05/14/115286.html