Lably
Skip to content

Run-time type information

Type information inspection taking place at runtime

  • 13 min read
  • 4 chapters

Related on Lably

Through Computer programming, Data type and Execution (computing)

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 dynamic_cast<T> operator, and to manipulate type information at runtime using the typeid operator and std::type_info class, which itself stores the runtime information. In Object Pascal, RTTI can be used to perform safe type casts with the as operator, test the class to which an object belongs with the is operator, and manipulate type information at run time with classes contained in the RTTI 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 in 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.

Sign in to keep reading

3 more chapters are free with a Lably account.

More in Computing and communication