Run Time Type Information (RTTI) in C++
Why RTTI?Up-casting (treating sub-class as base class ) works fine, for example,
Code:Animal * animal = new Dog();
However, down-casting might not be safe as correctness cannot be determined by the C++ compiler. See the following example.
Code:Dog * dog = (Dog*) animal;
So we need RTTI to support both up-casting and down-casting.
Mechanisms for RTTI1. dynamic_cast operator
2. typeid operator and type_info class
See details of dynamic_cast operator
here.
RTTI typeid operator• Obtains type info of an object/expression which is a reference to a standard library type called type_info.
• usage: typeid( object) - The usage is similar to “sizeofâ€.
• To get the name of the type, use typeid(object).name().