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.