RTTI

RTTI stands for Run Time Type Identification.In an inheritance hierarchy,using RTTI we can find the exact type of the object using a pointer or reference to the base class.

How to get the information about the object at run time?
There are two ways to get the information about the object at run time they are as follows:

  • Using typeid() operator
  • Using dynamic_cast operator
Example:

Class Sample
{
    //Code
};
void main()
{
     Sample s;
     cout<<typeid(s).name();
}

The operator typeid() takes an object s and returns a reference to a global const object of the type type_info,then function name() is called to get the name of the class of the object.To use the type_info objects reference we need to include the header file "typeinfo.h".

The dynamic_cast operator can also be used to get the information of the object at run time.

No comments:

Post a Comment