Article 2: It is best to use C + + transformation operators

Source: Internet
Author: User

The old-style C language transformation, there are shortcomings, mainly in a few two aspects: 1. It almost allows you to convert any type to any other type. 2. Transformation is difficult to identify. The syntax structure for legacy transformations consists of a pair of parentheses plus an object name, while the parentheses and object names are likely to be used anywhere in C + +.
To address the characteristics of the C legacy transformation, C + + introduces 4 new transformation operators (cast Operators): Static_cast, Const_cast, dynamic_cast, and reinterpret_cast.
Static_cast basically has the same power and meaning as the C legacy transformation, along with the same limitations.
Example:
int A, B;
...
Double C = ((double) a)/b; C language notation
Double C = static_cast<double> (a)/b; C + + static_cast notation
The other three C + + transformation operations applies are used for more focused purposes.
Const_cast is used to change the constants in an expression, and the most common use is to remove the constant attribute.
Dynamic_cast is used to perform a "secure down or cross-type transformation" of the inheritance system by converting the parent class type to a subclass type and returning the transformation result: An error returns null or throws an exception. It cannot be applied to a type that lacks virtual functions, nor can it change the constant nature of a type. If you want to perform a transformation operation for a type that does not involve an inheritance mechanism, you can use static_cast; To change the constants, you must use Const_cast.
The last transformation operator is reinterpret_cast, and the result of this operator transformation is always tied to the build platform. Therefore, reinterpret_cast is not portable. The most common use of reinterpret_cast is to convert a "function pointer" type, such as changing the return value of a function. In general, only in the case of a non-only use function pointer transformation.
If your compiler does not yet support these transformations, you can follow the traditional approach in the past. You can also use macros to define the way in which these new features are supported after compilation.
Such as:
#define STATIC_CAST (type, expr) ((type) expr)
If the compiler supports static_cast, you can modify the macro definition on the line, do not need to modify the use of the macro source.

Article 2: It is best to use C + + transformation operators

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.