C ++ primer notes (8)

Source: Internet
Author: User

When implicit type conversion occurs
1. In a mixed expression, the operands are converted to the same type.
2. the conditional expression is converted to the bool type.
3. Use an expression to initialize a variable or assign a value. The expression is converted to the type of the variable.

4. implicit type conversion may also occur in function calls.


Explicit conversions are also called forced conversions. Forced conversions include static_cast, dynamic_cast, const_cast, and reinterpret_cast;
When force conversion type is required: overwrite the standard conversion, and use force type conversion explicitly.
Double dval;
Int ival;
Ival * = dval;
To perform multiplication with dval, convert ival to the double type. Then, the result of the double type of multiplication operation is truncated to the int type and assigned to ival, to remove the unnecessary conversion from converting ival to double type, you can forcibly convert dval to int type.

Ival * = static_case <int> (dval );


Dynamic_cast supports recognition of pointers or references to objects during runtime.
Const_cast is used to replace the const attribute of the expression.
Any type conversions implicitly executed by the compiler can be explicitly completed by static_cast.

Reinterpret_cast generally provides a lower-level re-interpretation for the bit mode of the operand.


Strongly recommendedProgramAvoid forced type conversion.
Although Standard C ++ still supports legacy forced conversion symbols, we recommend that you only write them in the C language or compiler before Standard C ++.Code.

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.