设为首页 加入收藏

TOP

5.12.1 When Implicit Type Conversions Occur
2013-10-07 15:24:00 来源: 作者: 【 】 浏览:74
Tags:5.12.1 When Implicit Type Conversions Occur

尽管我们在写代码时应该避免隐式或显式的类型转换,但是有些转换是在不经意间发生的。读者应该熟悉发生隐式类型转换的五种场景。

The compiler applies conversions for both built-in and class type objects as necessary. Implicit type conversions take place in the following situations:

· In expressions with operands of mixed types, the types are converted to a common type:

  1. int ival;  
  2. double dval;  
  3. ival >= dval // ival converted to double 

· An expression used as a condition is converted to bool:

  1. int ival;  
  2. if (ival) // ival converted to bool  
  3. while (cin) // cin converted to bool 

Conditions occur as the first operand of the conditional ( :) operator and as the operand(s) to the logical NOT (!), logical AND (&&), and logical OR (||) operators. Conditions also appear in the if, while, for, and do while statements. (We cover the do while in Chapter 6)

· An expression used to initialize or assign to a variable is converted to the type of the variable:

  1. int ival = 3.14; // 3.14 converted to int  
  2. int *ip;  
  3. ip = 0; // the int 0 converted to a null pointer of type int * 

In addition, as we’ll see in Chapter 7, implicit conversions also occur during function calls.

】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
分享到: 
上一篇5.12.5 When Casts Might Be Usef.. 下一篇2.3.2 The Name of a Variable

评论

帐  号: 密码: (新用户注册)
验 证 码:
表  情:
内  容:

·Sphinx : 高性能SQL (2025-12-24 10:18:11)
·Pandas 性能优化 - (2025-12-24 10:18:08)
·MySQL 索引 - 菜鸟教 (2025-12-24 10:18:06)
·Shell 基本运算符 - (2025-12-24 09:52:56)
·Shell 函数 | 菜鸟教 (2025-12-24 09:52:54)