Skip to main content

Posts

Showing posts with the label RTTI

RTTI

Oh, do not confuse this word with RTI - right to information. This is not a political blog :). RTTI - run time type identification is a concept in C++, which gives the type of the object during run time. But it can be used only with polymorphic classes - i.e. the classes which have at least one virtual function. Typeid typeid is an operator in C++ which gives the actual type of the object. So what is actual type of object? Is it different from the one we have defined it with? It may be. class A { public: virtual void print(){} }; class B : public A { }; int main () { int a; A obj1; B obj2; A * ptr1 = new B; }  In the code above, type of a is integer. Type of obj1 is A. Type of obj2 is B. But what about *ptr1? Is the type of *ptr1 A or B? typeid operator correctly gives its type as B. Note : We have added a dummy print function in base class, to make A as polymorphic. Now let us use typeid operator - which needs the inclusion of ...