Typeid
Encyclopedia
In C++
, the
of an object
at run time. It returns a reference
to
Objects of class
It is generally only useful to use typeid on the dereference of a pointer or reference (i.e.
) because these are the only expressions that are associated with run-time type information. The type of any other expression is known at compile time.
Output (exact output varies by system):
Person
Employee
Person*
Employee
Employee
C++
C++ is a statically typed, free-form, multi-paradigm, compiled, general-purpose programming language. It is regarded as an intermediate-level language, as it comprises a combination of both high-level and low-level language features. It was developed by Bjarne Stroustrup starting in 1979 at Bell...
, the
typeid
keyword is used to determine the classClass (computer science)
In object-oriented programming, a class is a construct that is used as a blueprint to create instances of itself – referred to as class instances, class objects, instance objects or simply objects. A class defines constituent members which enable these class instances to have state and behavior...
of an object
Object (computer science)
In computer science, an object is any entity that can be manipulated by the commands of a programming language, such as a value, variable, function, or data structure...
at run time. It returns a reference
Reference (C++)
In the C++ programming language, a reference is a simple reference datatype that is less powerful but safer than the pointer type inherited from C...
to
std::type_info
object, which exists until the end of the program. The use of typeid
is often preferred over dynamic_cast<class_type>
in situations where just the class information is needed, because typeid
, applied on a type or non de-referenced value is a constant-time procedure, whereas dynamic castDynamic castIn the C++ programming language, the dynamic_cast operator is a part of the run-time type information system that performs a typecast. Unlike an ordinary C-style typecast, a type safety check is performed at runtime, and if the types are not compatible, an exception will be thrown or a null...
must traverse the class derivation lattice of its argument at runtime. However, you should not rely on the exact content, such as that returned by std::type_info::name
, as this is implementation specific with respect to the compiler.Objects of class
std::bad_typeid
are thrown when typeid
is called on a dereferenced null pointer. The class is derived from std::exception
, and thrown by typeid and others.It is generally only useful to use typeid on the dereference of a pointer or reference (i.e.
typeid(*ptr)
or typeid(ref)
) to an object of polymorphic class type (a class with at least one virtual functionVirtual function
In object-oriented programming, a virtual function or virtual method is a function or method whose behaviour can be overridden within an inheriting class by a function with the same signature...
) because these are the only expressions that are associated with run-time type information. The type of any other expression is known at compile time.
Example
Output (exact output varies by system):
Person
Employee
Person*
Employee
Employee