The C ++ polymorphism caused by virtual functions/inheritance makes C ++ProgramThere is no way to confirm the specific type of each object during compilation.
Corresponding to this, it is necessary to provide a method at runtime to determine the specific type of the object, that is, rtti, runtime type identifier.
In essence, the rtti compiler secretly hides a pointer in the virtual function table, pointing to a data structure that records the real type information of the class. That is, typ_info. Because this pointer is hidden in the virtual function table, it is obvious that all objects related to dynamic type determination are generated for objects with virtual inheritance (virtual.
Typeid
# Include < Typeinfo >
Typeid ( Object )
Typeid (object) is a function built on rtti. It is used to return the information of the actual object type, that is, the reference of a type_info class object.
The type_info class contains the following members:
Bool Opereator = ( Const Type_info & Ob );
Bool Opereator ! = ( Const Type_info & Ob );
Bool Before ( Const Type_info & Ob );
Const Char * Name ();
Typeid takes effect for the polymorphism type. That is, there is virtual in the class system.
In essence, typeid is based on rtti and does not have a virtual class system. The types can be determined during compilation. They are not governed by rtti.
Dynamic_cast
Dynamic_cast < Target - Type > (Expr)
Dynamic_cast will determine whether expr can be correctly converted to target-type, that is, using rtti to determine whether the target type is expr 1) Real Type 2) parent class or 3) parent .. the parent class is forced conversion. If not, it will not be converted and a bad_cast will be thrown.
Const_cast
Const_cast < Type > (Expr)
The meaning of const_cast has two opposite contexts.
1) if expr is originally const, remove const; 2) if there is no const, add Const.
Static_cast
That is, do not execute the forced conversion of type check. It can be viewed as a dynamic_cast that skips the rtti judgment. Improved efficiency.
Do not use dynamic_cast to secure static_cast. However, dynamic must be used to ensure security.
Reinterpret_cast
It doesn't matter whether it is converted between irrelevant types, such as pointer to integer, or between different types of pointers.