Inheritance/Polymorphism  «Prev  Next»
Lesson 15 Runtime Type Identification
Objective Define the three components of RTTI.

Runtime Type Identification in C++

Runtime Type Identification (RTTI) provides a mechanism for safely determining the type pointed at by a base class pointer at runtime.
RTTI involves
  1. dynamic_cast, an operator on a base class pointer
  2. typeid, an operator for determining the type of an object
  3. type_info, a structure providing runtime information for the associated type

We have already examined dynamic_cast. We will look at the other two components to RTTI in the next lesson.

(RTTI) Run-Time Type Information

(RTTI) Run-Time Type Information, or Run-Time Type Identification refers to a C++ mechanism that exposes information about an object's data type at runtime. Run-time type information can apply to simple data types, such as integers and characters, or to generic types. This is a C++ specialization of a more general concept known as type introspection. Similar mechanisms are also known in other programming languages, such as Delphi (Object Pascal).
In the original C++ design, Bjarne Stroustrup did not include run-time type information, because he thought this mechanism was frequently misused.


The dynamic_cast<> operation and typeid operator in C++ are part of RTTI. The C++ run-time type information permits performing safe typecasts and manipulate type information at run time. RTTI is available only for classes which have at least one virtual method. In practice, this is not a limitation because base classes must have a virtual destructor to allow objects of derived classes to perform proper cleanup if they are deleted from a base pointer. RTTI is optional with some compilers; the programmer can choose at compile time whether to include the function. There may be a resource cost to making RTTI available even if the program does not use it.