开发者

Mixing post- and pre- increment/decrement operators on the same variable [duplicate]

开发者 https://www.devze.com 2023-01-07 17:51 出处:网络
This question already has answers here: 开发者_如何学运维 Closed 12 years ago. Possible Duplicate:
This question already has answers here: 开发者_如何学运维 Closed 12 years ago.

Possible Duplicate:

Why is ++i considered an l-value, but i++ is not?

In C++ (and also in C), if I write:

++x--
++(x--)

i get the error: lvalue required as increment operand

However (++x)-- compiles. I am confused.


Post- and pre-increment operators only work on lvalues.

When you call ++i the value of i is incremented and then i is returned. In C++ the return value is the variable and is an lvalue.

When you call i++ (or i--) the return value is the value of i before it was incremented. This is a copy of the old value and doesn't correspond to the variable i so it cannot be used as an lvalue.

Anyway don't do this, even if it compiles.

0

精彩评论

暂无评论...
验证码 换一张
取 消