Static_cast and reinterpret_cast

Source: Internet
Author: User

Usage: static_cast <type-ID> (expression)

This operator converts expression to the Type-ID type, but does not check the runtime type 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 a subclass to a base class;

When performing a downstream conversion (converting a base class pointer or reference to a subclass), it is not safe because there is no dynamic type check.

② It is used for conversion 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.

③ Convert 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 attribute of expression.

Differences between static_cast and reinterpret_cast in C ++

In chapter 5 of C ++ primer, the compiler implicitly executes any type conversion, which can be displayed by static_cast. reinterpret_cast generally provides a lower-level re-interpretation for the bit mode of the operand.

1. static_cast in C ++ performs non-polymorphism conversion instead of the normal conversion operation in C. Therefore, it is used as an implicit type conversion. For example:

Int I;

Float F = 166.7f;

I = static_cast <int> (f );

In this case, the result is that the I value is 166.

2. reinterpret_cast in C ++ converts data from one type to another. The so-called "usually provides lower-level re-interpretation for the bit mode of the operand" means re-interpretation of the data in the form of binary existence. For example:

Int I;

Char * P = "this is a example .";

I = reinterpret_cast <int> (P );

In this case, the values of I and P are exactly the same. Reinterpret_cast is used to interpret the value of P as an integer in binary (bit mode) mode and assign it to I. An obvious phenomenon is that there is no digital loss before and after conversion.

Reinterpret_cast

Reinterpret_cast is the forced type conversion character in C ++.

The operator modifies the operand type, but only re-explains the BIT model of the given object without binary conversion.

For example, int * n = new int;

Double * D = reinterpret_cast <double *> (N );

After calculation, D contains useless values. This is because reinterpret_cast only copies N bits to d without necessary analysis.

Therefore, use reinterpret_cast with caution.

In addition, reinterpret_cast can only be converted between pointers.

Static_cast and reinterpret_cast

Reinterpret_cast is used to map to a completely different type. This keyword is used when we need to map the type back to the original type. The type we map to is only for xuanjicang and other purposes, which is the most dangerous of all mappings. (This sentence is the original statement in C ++ programming ideas)

The static_cast and reinterpret_cast operators modify the operand type. They are not reciprocal. static_cast performs conversions using type information during compilation, and performs necessary checks during conversions (such as cross-border pointer calculation and type check). Its operations are relatively safe. On the other hand, reinterpret_cast only reinterprets the BIT model of the given object without binary conversion. The example is as follows:

Int n = 9; double D = static_cast <double> (N );

In the above example, we convert a variable from int to double. These types of binary expressions are different. To convert an integer 9 to a double-precision integer 9, static_cast needs to complement the bit with a double-precision integer d correctly. The result is 9.0. The reinterpret_cast behavior is different:

Int n = 9;

Double D = reinterpret_cast <double &> (N );

This time, the results are different. After calculation, D contains useless values. This is because reinterpret_cast only copies N bits to D and does not perform necessary analysis.

Therefore, use reinterpret_cast with caution.

 

Note: static_cast cannot convert the const, volitale, or _ unaligned attribute of expression.

 

Reinterpret_cast brute force conversion is equivalent to forced conversion in C.
Static_cast cannot convert the function pointer.

 

The reinterpret_cast operator cannot cast away the const, volatile, or _ unaligned attributes.

When this operator is used for type conversion, almost all the conversion results are the execution period definition (implementation-defined ). ThereforeCodeIt is difficult to transplant. The most common purpose of reinterpret_casts is to convert between function pointer types.

For example, the Code for converting function pointers cannot be transplanted (C ++ does not guarantee that all function pointers are represented in the same way ), in some cases, such conversions may produce incorrect results (see the m31 clause), so you should avoid converting the function pointer type.

1. static_cast in C ++ performs non-polymorphism conversion instead of the normal conversion operation in C. Therefore, it is used as an implicit type conversion. For example, int I; float F = 166.7f; I = static_cast <int> (f); in this case, the I value is 166. 2. reinterpret_cast in C ++ converts data from one type to another. The so-called "usually provides lower-level re-interpretation for the bit mode of the operand" means re-interpretation of the data in the form of binary existence. For example, int I; char * P = "this is a example."; I = reinterpret_cast <int> (p); in this case, the I and P values are exactly the same. Reinterpret_cast is used to interpret the value of P as an integer in binary (bit mode) mode and assign it to I. An obvious phenomenon is that there is no digital loss before and after conversion.

 
 
 
The parent class Pointer Points to a subclass instance. It cannot access its own members, but can only access inherited or overloaded members (functions ).

Dynamic_cast is used to convert classes in an integrated system with polymorphism (with virtual functions) and convert subclass pointers to parent class pointers, this pointer can only access the functions of the parent class, because the information is determined during compilation rather than during runtime. If the parent class pointer is converted to a subclass pointer using the modified operator, the NULL pointer is returned.

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.