C++ provides RTTI (Runtime Type Information ) mechanism by which we can get information of an object or variable at run time. In c++ we can get information about object or variable in two ways

To use typeid() operator  we need to include #include <typeinfo> in the source code. The typeid operator returns a const reference to a type_info object. Below is a simple example of typeid use.


The object class name is : 1A

  • The typeid can be used to determine whether two objects are the same type.
  • When argument to typeid is pointer to a polymorphic class, the result is the type of the most derived object.

  • When the argument to typeid is a pointer to base class which points to derived class and if base class is not polymorphic then result refers to a std::type_info object representing the static type of the expression. 

Related Contents to follow