4.11 Type Conversions

1.The compiler automatically converts operands in the following circumstances:

  • In most expressions, values of integral types smaller than int are first promoted to an appropriate larger integral type.
  • In conditions, nonbool expressions are converted to bool.
  • In initializations, the initializer is converted to the type of the variable; in the assignments, the right-hand operand is converted operand is converted to the type of the left-hand.
  • In arithmetic and relational expressions with operands of mixted types, the types are converted to a common type.
  • As we'll see in Chapter 6, conversions also happen during function calls.

The implicit conversions among the arithmetic types are defined to preserve precision.Example:

int ival = 3.541 + 3;

First, 3 is converted double type,then 3.541 + 3,and the result is a double, next the type of the object we are initializing dominates,the (3.541 + 3)double is converted to the integral type.Finally the result is integer.

4.11.1 The Arithmetic Conversions

1.Operands to an operator are converted to the widest type.

2.The integral promotions converted the small integral types to a lager integral type .(The types bool, char ,signed char, unsigned char, short, and unsigned short are promoted to int if all possible values of that type fit in an int.

3.The lager char types(wchar_t, char16_t,and char32_t) are promoted to the smallest type of int, unsigned int, long, unsigned longm, long long, or usigned long long in which all possiable values  of that character type fit.

4.one way to understand the arithmetic conversions is to study lots of examples.

4.11.2 Other Implicit Conversions

1.Array to Pointer Conversions    

   Pointer Conversions :A constant integral value of 0 and the literal nullptr can be converted to any pointer type ,a pointer to any nonconst type can be converted to void*, and a pointer to any type can be converted to a const void*.

   Conversions to  bool

 Conversion to const:We can convert a pointer to an nonconst type to a pointer to the corresponding const type, and similarly for references.

 The reserse conversions——removing a low-level const ——dose not exist.

 Conversions Define By Class Types:We use a clas-type conversion when we use C-style character string where a library string is expected and when we read from an istream in a condition.

 example:

string s = "hello";//"hello" is string literal which is converted to string
while(cin << s)//input success,istream is converted to bool

4.11.3 Explicit Conversions

1.Named Casts

cast-name<type>(expression);

2.static_cast

Any well-defined type conversion,other than those involving low-level const, can be requested using a static_cast.

Compilers often generate a warning for assignments of a larger arithmetic type to a smaller type.When we do an explicit cast, the warning message is turned off.

3.const _cast

if the object was originally not a const, using a cast to obtain write access is legal.However, using a const_cast in order to write to a const object id defined.

Only a const_cast may be used to change the constness of a expression.

We cannot use a const_cast to change the type of an expression.

4.reinterpret_cast

A reinterpret_cast generally performs a low_level reinterpretation of the bit pattern of its operands.

Cast interfere with normal type checking.Avoiding casts.

猜你喜欢

转载自www.cnblogs.com/Mayfly-nymph/p/9130132.html