RTTI (Run-Time Type Information, or Run-Time Type Identification refers to a 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 called type introspection.
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.
class Base
{
public:
virtual ~Base(){}
};
class Derived : public Base
{
public:
Derived() {}
virtual ~Derived() {}
int compare (Derived& ref);
};
int myComparisonMethodForGenericSort (Base& ref1, Base& ref2)
{
Derived& d = dynamic_cast<Derived&>(ref1); //RTTI used here
//Note: If the cast is not successful, RTTI enables the process to throw a bad_cast exception
return d.compare (dynamic_cast<Derived&>(ref2));
}
Ref: https://en.wikipedia.org/wiki/Run-time_type_information