In C++, RTTI can be used to do safe typecasts, using the dynamic_cast<> operator, and to manipulate type information at runtime, using the typeid operator and std::type_infoclass. RTTI is available only for classes that are polymorphic, which means they 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 functionality. There may be a resource cost to making RTTI available even if a program does not use it.
typeid
The typeidkeyword is used to determine the class of an object at run time. It returns a reference to std::type_info object, which exists until the end of the program. The use of typeid, in a non-polymorphic context, is often preferred over dynamic_cast<class_type> in situations where just the class information is needed, because typeid is always a constant-time procedure, whereas dynamic_cast may need to traverse the class derivation lattice of its argument at runtime. Some aspects of the returned object are implementation-defined, such as std::type_info::name, and cannot be relied on across compilers to be consistent. Objects of class std::bad_typeid are thrown when the expression for typeid is the result of applying the unary * operator on a null pointer. Whether an exception is thrown for other null reference arguments is implementation-dependent. In other words, for the exception to be guaranteed, the expression must take the form typeid where p is any expression resulting in a null pointer.
Example
include
include
class Person ; class Employee : public Person ; int main
Output : Person Employee Person* Employee Employee
The dynamic_cast operator in C++ is used for downcasting a reference or pointer to a more specific type in the class hierarchy. Unlike the static_cast, the target of the dynamic_cast must be a pointer or reference to class. Unlike static_cast and C-styletypecast, a type safety check is performed at runtime. If the types are not compatible, an exception will be thrown or a null pointer will be returned. A Java typecast behaves similarly; if the object being cast is not actually an instance of the target type, and cannot be converted to one by a language-defined method, an instance of java.lang.ClassCastException will be thrown.
Example
Suppose some function takes an object of type A as its argument, and wishes to perform some additional operation if the object passed is an instance of B, a subclass of A. This can be accomplished using dynamic_cast as follows.
include
include
include
include
using namespace std; class A ; class B: public A ; void MyFunction int main
Console output: Method specific for B was invoked Method specific for B was invoked Exception std::bad_cast thrown. Object is not of type B A similar version of MyFunction can be written with pointers instead of references: void MyFunction