typeid
typeid allows to check the type of an expression: typeid (expression)This operator returns a reference to a constant object of type
type_info that is defined in the standard header<typeinfo>. A value returned by typeid can be compared with another value returned by typeid using operators == and!= or can serve to obtain a null-terminated character sequence representing the data type or class name by using itsname() member.
|
| a and b are of different types: a is: int * b is: int |
When
typeid is applied to classes, typeid uses the RTTI to keep track of the type of dynamic objects. When typeid is applied to an expression whose type is a polymorphic class, the result is the type of the most derived complete object:
|
| a is: class Base * b is: class Base * *a is: class Base *b is: class Derived |
Note: The string returned by member
name of type_info depends on the specific implementation of your compiler and library. It is not necessarily a simple string with its typical type name, like in the compiler used to produce this output. Notice how the type that
typeid considers for pointers is the pointer type itself (both a and b are of type class Base *). However, when typeid is applied to objects (like *a and *b) typeid yields their dynamic type (i.e. the type of their most derived complete object).If the type
typeid evaluates is a pointer preceded by the dereference operator (*), and this pointer has a null value,typeid throws a bad_typeid exception.
No comments:
Post a Comment