Rtti and four forced conversion Operators

Source: Internet
Author: User

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.

 

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.