How does Run-Time Type Information (RTTI) allow a program to determine an object's actual type at runtime?
Last updated
RTTI (Run-Time Type Information) keeps type metadata attached to objects (e.g. via the vtable in C++ or the class object in Java/C#), revealing the actual dynamic type behind a reference's declared static type.
Operators query this metadata:
typeid/dynamic_cast(C++),instanceof/getClass()(Java),is/GetType()(C#). Checked casts return null or throw instead of causing undefined behavior.In C++ it requires polymorphic types and adds slight overhead. Heavy type-switching on RTTI is a design smell, so prefer virtual dispatch.