好久都说要好好测测typeid的用法,今天终于得以如愿以偿,呵呵。下面,我就把测试代码发下来,
#include <typeinfo>
#include <iostream>
using namespace std;
class Base
{
public:
virtual void vvFcn(){}
};
class Derived : public Base
{
public:
virtual void vvFcn(){}
};
int main()
{
Derived *pd = new Derived;
Base *pb = pd;
cout << typeid(pb).name() << endl;
cout << typeid(*pb).name() << endl;
cout << typeid(pd).name() << endl;
cout << (typeid(int) == typeid(int&)) << endl;
return 0;
}
测试结果如下所示,
class Base*
class Derived
class Derived*
1