Static_cast, dynamic_cast, reinterpret_cast, const_cast, pointer, forced conversion of data types

Source: Internet
Author: User

Many books have discussed the issue of forced type conversion. The most detailed issue is C ++.
The father of C ++'s design and evolution. The best solution is not to use a C-style forced type conversion, but to use the standard C ++ type conversion operator static_cast and dynamic_cast. There are four types of Conversion characters in Standard C ++: static_cast, dynamic_cast, reinterpret_cast, and const_cast. Next we will introduce them one by one.

Static_cast

Usage: static_cast <type-ID> (expression_r_r)

This operator converts expression_r_r to the Type-ID type, but there is no runtime type check to ensure the conversion security. It has the following usage:
It is used to convert pointers or references between classes and subclasses in the class hierarchy. It is safe to perform upstream conversion (converting the pointer or reference of the subclass to the base class representation). When performing downstream conversion (converting the base class pointer or reference to the subclass representation, because there is no dynamic type check, it is not safe.
It is used to convert between basic data types. For example, convert int to Char and convert int to enum. The security of such conversions must also be ensured by developers.
Converts a null pointer to a null pointer of the target type.
Convert any type of expression to void type.
Note: static_cast cannot convert the const, volitale, or _ unaligned attributes of expression_r_r.

Dynamic_cast

Usage: dynamic_cast <type-ID> (expression_r_r)

This operator converts expression_r_r to an object of the Type-ID type. Type-ID must be a class pointer, class reference, or void *. If type-ID is a class pointer type, expression_r_r must also be a pointer, if type-ID is a reference, expression_r_r must also be a reference.

Dynamic_cast is mainly used for upstream and downstream conversions between classes, and can also be used for cross conversions between classes.

When performing upstream conversion between classes, dynamic_cast and static_cast have the same effect. During downstream conversion, dynamic_cast has the type check function, which is safer than static_cast.

class B{public:int m_iNum;virtual void foo();};class D:public B{public:char *m_szName[100];}; void func(B *pb){D *pd1 = static_cast<D *>(pb);D *pd2 = dynamic_cast<D *>(pb);}

In the above Code segment, if PB points to a D-type object, pd1 and Pd2 are the same, and it is safe to execute any operations of the D-type on these two pointers; however, if Pb points to a B-type object, pd1 will be a pointer to this object, and operations of Type D on it will be insecure (for example, accessing m_szname ), pd2 is a null pointer. Note: B must have virtual functions; otherwise, compilation errors may occur. static_cast does not have this restriction. This is because the runtime type check requires runtime type information, which is stored in the virtual function table of the class (the concept of the virtual function table can be seen in detail <inside
In C ++ object model>), only classes that define virtual functions have virtual function tables. classes that do not define virtual functions do not have virtual function tables.

In addition, dynamic_cast also supports cross cast ). The following code is used.

class A{public:int m_iNum;virtual void f(){}}; class B:public A{}; class D:public A{}; void foo(){B *pb = new B;pb->m_iNum = 100;D *pd1 = static_cast<D *>(pb); //copile errorD *pd2 = dynamic_cast<D *>(pb); //pd2 is NULLdelete pb;}

In function Foo, using static_cast for conversion is not allowed, and errors will occur during compilation. Using dynamic_cast for conversion is allowed, and the result is a null pointer.

Reinpreter_cast

Usage: reinpreter_cast <type-ID> (expression_r_r)

Type-ID must be a pointer, reference, arithmetic type, function pointer, or member pointer. It can convert a pointer to an integer, or an integer to a pointer (first, convert a pointer to an integer, and then convert the integer to the original type of pointer, you can also get the original pointer value ).

This operator is used in many ways.

Const_cast

Usage: const_cast <type_id> (expression_r_r)

This operator is used to modify the const or volatile attributes of the type. Apart from const or volatile modification, type_id and expression_r_r are of the same type.

Constant pointers are converted to non-constant pointers and still point to the original objects. Constant references are converted to non-constant references and still point to the original objects; constant objects are converted to very large objects.

Voiatile and const classes. Take the following example:

class B{public:int m_iNum;}void foo(){const B b1;b1.m_iNum = 100; //comile errorB b2 = const_cast<B>(b1);b2. m_iNum = 200; //fine}

The above code will report an error during compilation, because B1 is a constant object and cannot be changed. Use const_cast to convert it into a constant object, you can change its data members at will. Note: B1 and B2 are two different objects.

The easiest to understand:

Static_cast:
If you don't know which one to use, use this conversion.  

Dynamic_cast:
It is usually used for conversion between the base class and the derived class;
Reinterpret_cast:
Converts a character pointer to an integer. 

Const_cast:Mainly for const and volatile conversion.  

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.