天天看點

C++ RTTI(typeid)

#include <iostream.h>

#include <typeinfo.h>

class A

{

public:

A(){};

~A(){};

};

class B

{

public:

B(){};

~B(){};

};

template <class T>

void Gettype(T* x)

{

const type_info& pInfo = typeid(x);

cout<<pInfo.name()<<endl;

}

int main()

{

A* pA = new A();

B* pB = new B();

A a;

B b;

Gettype(pA); //class A*

Gettype(pB); //class B*

Gettype(&a); //class A*

Gettype(&b); //class B*

return 0;

}