typeid 能动态的寻找当前的对象,实现多态,例如:
...
#include<typeidinfo>
class base
{
public:
void iam()
{
cout << typeid(*this).name() << endl;
}
virtual void polo()
{
cout << "OK" << endl;
}
};
class base1 :public base
{
/*
public:
virtual void iam()
{
cout << "base1" << endl;
}
*/
};
int main(void)
{
base a;
base1 b;
a.iam();
b.iam();
}
将各自显示各自的类名称。
1.如果在父类中没有虚函数virtual void polo,则父类不再具有多态性质,则typeid也将不再发生作用