Four types of C + + conversion methods.

Source: Internet
Author: User

Type conversion has C-style, and of course, C + + style. C-style conversion format is very simple (type) EXPRESSION, but C-style type conversion has a lot of shortcomings, sometimes with C-style conversion is not appropriate, because it can be converted between any type, For example, you can convert a pointer to a const object into a pointer to a non-const object, convert a pointer to a base class object to a pointer to a derived class object, the difference between the two transformations is huge, but the traditional C-style type conversion does not differentiate between these exponentially. Another drawback is that C-style conversion is not easy to find, he consists of a parenthesis plus an identifier, and such things in the C + + program a lot. So C + + in order to overcome these shortcomings, the introduction of 4 new type conversion operators, they are 1.static_cast 2.const_cast 3.dynamic_cast 4.reinterpret_cast.

1. STATIC_CAST (exp) display conversion

Static_cast type conversions are used for conversions between related types, such as converting up or down in the inheritance hierarchy of the same class, conversions between enum types and integer types, and conversions between floating-point types and exponential types.

In these 4 type conversions, Static_cast is the closest to C-style.

2, Reinterpret_cast (exp) Explicit conversion

Literal understanding is re-interpret, re-interpretation (interpretation) of the meaning. Therefore, it is mainly used for the conversion between unrelated types, good one English words in different contexts, part of speech and meaning may be completely different. It facilitates the conversion between different types, but is also associated with risk, such as converting a hexadecimal integer to a memory address (which is completely unrelated by the int--> pointer type). Since it is used for conversions between unrelated types, it means that the compiler does not make too many confirmations and promises.

Another feature of the reinterpret_cast approach is that there is at least the same number of bits between the target and the original value, and we can convert the converted value back, unlike the other 3 types, which can result in loss of precision.

3. DYNAMIC_CAST (exp) Display conversion

A type conversion for runtime (run-time) detection, so the conversion may require a large runtime cost, and this type is not possible with C-style. Primarily used to perform conversions between type-down conversions and inheritance. Dynamic_cast and static_cast have the same basic syntax, dynamic_cast is primarily used for upstream and downstream conversions between class hierarchies, and can also be used for cross-conversion between classes. The effect of dynamic_cast and static_cast is the same when upstream conversions are made between class hierarchies, and dynamic_cast has the function of type checking, which is more secure than static_cast when making a downstream conversion.

4. CONST_CAST (exp) Display conversion

To eliminate the const qualification of a variable, the converted variable will no longer have a "const", and if it is a const pointer, it can be changed to point to other objects.

  

The format conversion of C-style (Type) expression can be replaced by the 4 ways we described above. Although it is still available in the form of C-style, it is more recommended to use the above 4 type. Because the type conversion of the C-style is more dangerous:

More difficult to locate and identify in a large program

What type of conversion is not obvious

The four ways above can be used to more accurately locate which type of conversion has been wrong.

Four types of C + + conversion methods.

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.