C++系列文章:17++或是--18,可以这样写吗?(2007-04-25 09:32:00)

来源:互联网 发布:如何戒淘宝成瘾 编辑:程序博客网 时间:2024/05/22 08:16

不能这样写,后缀++,--要求操作数是一个可以被修改的左值(lvalue)。前缀++,--要求操作数是一个可以被修改的右值(rvalue)。问题中的17,18这样的字面常量值是不行的,编译时会出现错误。
下面是C++ 98标准中的一段话。可认参考

The value obtained by applying a postfix ++ is the value that the operand had before applying the operator.[Note: the value obtained is a copy of the original value ] The operand shall be a modifiable lvalue. The type of the operand shall be an arithmetic type or a pointer to a complete object type. After the result is noted, the value of the object is modified by adding 1 to it, unless the object is of type bool, in which case it is set to true. [Note: this use is deprecated, see annex D. ] The result is an rvalue. The type of the result is the cvunqualified version of the type of the operand. See also 5.7 and 5.17.
The operand of postfix is decremented analogously to the postfix ++ operator, except that the operand shall not be of type bool. [Note: For prefix increment and decrement, see 5.3.2. ]

原创粉丝点击