RTTI (Run-Time Type Information) in C++
In C++, RTTI or Run-Time Type Information reveals information about the data type of an object at runtime and only works with classes that have at least one virtual function. It enables the type of an object to be determined while the application is running.
Let's say a class descends from a base class that includes virtual functions. In that situation, the C++ language mandates that a pointer to the base class type can call the virtual function implementations in the derived class object. Occasionally, a class containing virtual functions is referred to as a "polymorphic class."
Durational Casts
The simplest method to determine the runtime type of an item that used a pointer or reference is the runtime cast, which verifies that the cast is legal. This is useful when we need to cast a reference from a class name to a derived type. Casting an object is frequently required when working with the class inheritance structure.
Casting might be of two different kinds:
- Upcasting: It is when a pointer or reference to a derived class object is considered a base class pointer.
- Downcasting: The conversion of a reference or pointer to a derived class from a pointer to a base class.
To downcast a base class pointer to a child class in an inheritance tree, utilize "dynamic cast." Unless we attempt to cast an incorrect type, such as an object reference that is not of the kind of the intended subclass, casting correctly produces a pointer of a converted type.
Runtime data types for objects are only available to classes with at least one virtual function. During program execution, it enables the type of an object to be determined.
Because base class B is missing a virtual function, the following program encounters the error "cannot dynamic convert 'b' (of type 'class B*' to type 'category D*' (classifier is not polymorphic)." This is because dynamic cast requires RTTI. See the following C++ program.
Example:
// Run Time Type Identification (RTTI)
// however, lacking a virtual function
#include <iostream>
usingnamespace std;
// however, lacking a virtual function
class B {};
// setting up a derived class
class D : public B {};
// Driver Number
int main()
{
B* b = new D; // pointer to base class
D* d = dynamic_cast<D*>(b); // class pointer derived
if (d != NULL)
cout << "works";
else
cout << "cannot cast B* to D*";
getchar(); // for the subsequent character
return 0;
}
It works by giving the base class B a virtual function.
// a C++ application to show
// Successful Run Time Type Identification
// virtual capability
#include <iostream>
usingnamespace std;
// setting up the base class
class B {
virtualvoid fun() {}
};
// the beginning of a derived class
class D: public B {
};
// Driver Number
int main()
{
B* b = new D; // pointer to base class
D* d = dynamic_cast<D*>(b); // class pointer derived
if (d != NULL)
cout << "works";
else
cout << "cannot cast B* to D*";
getchar();
return 0;
}
Output:

RTTI abuses
In C++ projects, RTTI should only be applied sparingly. This is due to several factors. Most notably, RTTI is rarely better than other language techniques like polymorphism and templates. Although there are always exceptions, the general norm for RTTI is like that for go-to statements. Refrain from utilizing it to avoid using a good, more reliable design. Apply RTTI only when it makes sense, and you are confident in using it.
Limitations
The RTTI has various restrictions. First, only polymorphic types can be utilized with RTTI. This means that either directly or indirectly through inheritance, each of your classes must contain at least one virtual function. Second, certain compilers need a special option to enable RTTI because of the extra data needed to store types.
Be aware that under RTTI, references to pointers will not function:
void example( int*& reforest )
{
std::cout << "What type is *&refptrTest : " <<typeid( refptrTest ).name() << std::endl;
}
As typeid() does not accept reference types, it will report int*.
Note: Before using operator typeid within a compilation unit, the header file <type info> must be included according to the C++98 standard. The software is deemed incomplete if not.