Run-time type information

In computer programming, run-time type information or run-time type identification (RTTI) is a feature of some programming languages (such as C++, Object Pascal, and Ada ) that exposes information about an object's data type at runtime. Run-time type information may be available for all types or only to types that explicitly have it (as is the case with Ada). Run-time type information is a specialization of a more general concept called type introspection.

In the original C++ design, Bjarne Stroustrup did not include run-time type information, because he thought this mechanism was often misused.

Overview
In C++, RTTI can be used to do safe typecasts using the  operator, and to manipulate type information at runtime using the   operator and   class. In Object Pascal, RTTI can be used to perform safe type casts with the  operator, test the class to which an object belongs with the   operator, and manipulate type information at run time with classes contained in the   unit (i.e. classes: TRttiContext, TRttiInstanceType, etc.). In Ada, objects of tagged types also store a type tag, which permits the identification of the type of these object at runtime. The  operator can be used to test, at runtime, if an object is of a specific type and may be safely converted to it.

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.

Some compilers have flags to disable RTTI. Using these flags may reduce the overall size of the application, making them especially useful when targeting systems with a limited amount of memory.

C++ – typeid
The  reserved word (keyword) is used to determine the class of an object at runtime. It returns a reference to  object, which exists until the end of the program. The use of, in a non-polymorphic context, is often preferred over   in situations where just the class information is needed, because   is always a constant-time procedure, whereas   may need to traverse the class derivation lattice of its argument at runtime. Some aspects of the returned object are implementation-defined, such as, and cannot be relied on across compilers to be consistent.

Objects of class  are thrown when the expression for   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  where   is any expression resulting in a null pointer.

Example
Output (exact output varies by system and compiler):

Person Employee Person* Employee Employee

 C++ – dynamic_cast and Java cast
The  operator in C++ is used for downcasting a reference or pointer to a more specific type in the class hierarchy. Unlike the, the target of the   must be a pointer or reference to class. Unlike  and C-style typecast (where type check occurs while compiling), a type safety check is performed at runtime. If the types are not compatible, an exception will be thrown (when dealing with references) or a null pointer will be returned (when dealing with pointers).

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  will be thrown.

Example
Suppose some function takes an object of type  as its argument, and wishes to perform some additional operation if the object passed is an instance of , a subclass of. This can be done using  as follows.

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  can be written with pointers instead of references:

Object Pascal, Delphi
In Object Pascal and Delphi, the operator  is used to check the type of a class at runtime. It tests the belonging of an object to a given class, including classes of individual ancestors present in the inheritance hierarchy tree (e.g. Button1 is a TButton class that has ancestors: TWinControl → TControl → TComponent → TPersistent → TObject, where the latter is the ancestor of all classes). The operator  is used when an object needs to be treated at run time as if it belonged to an ancestor class.

The RTTI unit is used to manipulate object type information at run time. This unit contains a set of classes that allow you to: get information about an object's class and its ancestors, properties, methods and events, change property values and call methods. The following example shows the use of the RTTI module to obtain information about the class to which an object belongs, creating it, and to call its method. The example assumes that the TSubject class has been declared in a unit named SubjectUnit.