Four types of conversion

Source: Internet
Author: User

1.Static_cast

The most common type conversion operator, which is a type conversion under normal conditions, such as converting int to float, such as: int I; float F; F = (float) I; or F = static_cast <float> (I );

· Static _ cast can be used to force implicit conversions (for example, converting a non-const object to a const object, or converting an int to a double object ), it can also be used for many reverse conversions (for example, void * pointer transformation to type pointer, base class pointer transformation to derived class pointer ), however, it cannot transform a const object to a non-const object (only const_cast can do it), and it is closest to the C-style conversion.

2.Const_cast

Used to retrieve the const attribute and change the const type pointer to a non-const type pointer, for example, const int * Fun (int x, int y) {} int * PTR = const_cast <int *> (fun (2.3 ))

3.Dynamic_cast

That is, dynamic_cast can be used for the downward transformation in the inheritance system, which converts the base class pointer to the derived class pointer, Which is stricter and safer than static_cast. Dynamic_cast is less efficient than static_cast in execution, but static_cast can complete ing in a wider range. This unrestricted ing is accompanied by insecurity.

4.Reinterpret_cast

Interpret indicates the meaning of interpretation. reinterpret indicates a re-interpretation. This identifier indicates a re-interpretation of the data in binary format, but does not change its value. For example: int I; char * PTR = "Hello freind! "; I = reinterpret_cast <int> (PTR); this conversion method is rarely used.

 

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 must complement the bitwise of the double-precision integer d correctly. the result is 9.0. the reinterpret_cast behavior is different:

Four types of 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.