Four cast of C + +

Source: Internet
Author: User

1. C-style forced conversion

int A=1;char b= (char) A;

This conversion applies to the process-oriented non-class concept of C-language conversion, however such a translator can be applied indiscriminately to classes and classes of pointers, there is no security check.

2. const_cast

Usage:const_cast<type_id> (expression)

The const or volatile property used to modify the type, which is generally used to force the object's constants to be eliminated, and C does not provide a mechanism to eliminate this const

3. static_cast

Usage:static_cast<type_id> (expression)

The conversion is similar to the C-style conversion, there is no run-time type check, so the security of the conversion cannot be guaranteed. There are several ways to use the main:

(1) For basic data types, or Non_const to const (which in turn must be const_cast)

(2) Convert a null pointer to a pointer of the target type

(3) Convert any type of expression to void type

(4) A pointer to a subclass type can be converted to a pointer of the parent class type (the security of this conversion requires the developer to guarantee)

4. dynamic_cast

Usage:dynamic_cast<type*> (expression)

He is used only for objects and references, and is primarily used to perform a security-down transformation, where he can convert a pointer to a child class to a subclass pointer, but requires the parent class to have a virtual function, or null if the conversion to a pointer type fails, or to run out of the bad_cast exception if the reference type conversion fails

5. Reinpreter_cast

Usage:reinpreter_cast<type_id> (expression)

TYPE_ID can be a pointer, reference, arithmetic type, function pointer, or member pointer, this operator can be converted between non-related types, the operation is simply a worthwhile binary copy from one pointer to another, and the content pointed to between types does not have any type of check and conversion.

Note: The difference between static_cast and dynamic_cast:

A. Static does not perform security checks at the time of conversion, it is entirely up to the developer to consider that the dynamic will perform security checks when converting, and if the conversion of a pointer type fails to return null, then a Bad_cast exception is run if the conversion of the reference type fails.

B. Static is primarily used for conversions between value types, and dynamic can only be used for object pointers and reference cast,dynamic is a downward conversion, and requires the parent class to have a virtual function, otherwise compile an error.

Four cast of C + +

Related Article

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.