RTTI

来源:互联网 发布:公司单页网站源码 编辑:程序博客网 时间:2024/06/07 14:29
RTTI(run-time type identification) ,like exceptions ,depends on type information residing in the virtual function table. If you try to use RTTI on a class that has no virtual functions, you will get unexpected results.
for example:
//C24RTTIWithoutPolymorphism.cpp#include <cassert>#include <typeinfo>#include <iostream>using namespace std;class X { int i;public: // ...};class Y : public X {  int j;public: // ...};int main() {  X* xp = new Y;  // the two below condition is true;  assert(typeid(*xp) == typeid(X));  assert(typeid(*xp) != typeid(Y));  cin.get();}
So  RTTI is intended for use only with polymorphic class. Without virtual function table, the typeid() will not work;