设为首页 加入收藏

TOP

5.4 Assignment Operators
2013-10-07 15:26:25 来源: 作者: 【 】 浏览:67
Tags:5.4 Assignment Operators

The left-hand operand of an assignment operator must be a nonconst lvalue. Each of these assignments is illegal:

  1. int i, j, ival;  
  2. const int ci = i; // ok: initialization not assignment  
  3. 1024 = ival; // error: literals are rvalues  
  4. i + j = ival; // error: arithmetic expressions are rvalues  
  5. ci = ival; // error: can’t write to ci 

Array names are nonmodifiable lvalues: An array cannot be the target of an assignment. Both the subscript and dereference operators return lvalues. The result of dereference or subscript, when applied to a nonconst array, can be the left-hand
operand of an assignment:

  1. int ia[10];  
  2. ia[0] = 0; // ok: subscript is an lvalue  
  3. *ia = 0; // ok: dereference also is an lvalue 

The result of an assignment is the left-hand operand; the type of the result is the type of the left-hand operand.

C++(www.cppentry.com) 中,赋值是一种表达式,其值是(赋完值之后的)左侧操作数,其类型与左侧操作数(也就是被赋值的对象) 相同,其副作用是改变左侧操作数的值。这是本章出现的第一个有副作用的表达式,而前面讲的表达式都不会改变操作数本身(重载之后用于输出的左移表达式除外)。

The value assigned to the left-hand operand ordinarily is the value that is in the right-hand operand. However, assignments where the types of the left and right operands differ may require conversions that might change the value being assigned. In such cases, the value stored in the left-hand operand might differ from the value of the right-hand operand:

  1. ival = 0; // result: type int value 0  ival = 3.14159; // result: type int value 3  

Both these assignments yield values of type int. In the first case the value stored in ival is the same value as in its right-hand operand. In the second case the value stored in ival is different from the right-hand operand.

】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
分享到: 
上一篇5.9 Comma Operator 下一篇2.6 Typedef Names

评论

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

·哈希表 - 菜鸟教程 (2025-12-24 20:18:55)
·MySQL存储引擎InnoDB (2025-12-24 20:18:53)
·索引堆及其优化 - 菜 (2025-12-24 20:18:50)
·Shell 中各种括号的 (2025-12-24 19:50:39)
·Shell 变量 - 菜鸟教 (2025-12-24 19:50:37)