简体   繁体   English

增量和减量运算符

[英]Increment and Decrement operators

How are the below valid and invalid as shown and what do they mean. 如何显示以下有效和无效以及它们的含义。 When would such a situation arise to write this piece of code. 何时会出现这样的情况来编写这段代码。

++x = 5;     // legal
--x = 5;     // legal
x++ = 5;     // illegal
x-- = 5;     // illegal

The postfix (x++/x--) operators do not return an lvalue (a value you can assign into). 后缀(x ++ / x--)运算符不返回左值(可以赋值的值)。

They return a temporary value which is a copy of the value of the variable before the change 它们返回一个临时值,该值是更改前变量值的副本

The value is an rvalue, so you could write: 该值是右值,因此您可以写:

y = x++ and get the old value of x y = x++并获取y = x++的旧值

鉴于operator =()和operator ++()都可以重载,如果不了解运算符所应用的类型,就不可能说出代码的作用。

Those all modify the value of x more than once between sequence points, and are therefore undefined behavior, which you should carefully avoid. 这些都在序列点之间多次修改x的值,因此是未定义的行为,您应该小心避免。 I don't see where the distinction between "legal" and "illegal" comes in - since the behavior is legal, any behavior (including sending assorted email to the Secretary of State) is perfectly in accordance with the Standard. 我不知道“合法”和“非法”之间的区别在哪里 - 因为行为是合法的,任何行为(包括向国务卿发送各种电子邮件)都完全符合标准。

Assuming that the question is about built-in ++ and -- operators, none of these statements are strictly legal. 假设问题是关于内置的++--运算符,这些语句都不是严格合法的。

The first two are well-formed , ie they merely compilable because the result of prefix increment is lvalue. 前两个是格式良好的 ,即它们只是可编译的,因为前缀增量的结果是左值。 The last two are ill-formed , since the result of postfix increment is not a rvalue, which is why you can't assign to it. 最后两个是格式错误的 ,因为后缀增量的结果不是右值,这就是你无法分配它的原因。

However, even the first two are not legal in a sense that they produce undefined behavior. 然而,即使前两个在某种意义上也不合法,它们会产生不确定的行为。 It is illegal to modify the same object more than once without an intervening sequence point. 在没有插入序列点的情况下多次修改同一对象是非法的。 (Note also, that compilers are allowed to refuse to compile well-formed code that produces undefined behavior, meaning that even the first pair might prove to be non-compilable). (另请注意,允许编译器拒绝编译生成未定义行为的格式良好的代码,这意味着即使第一对代码也可能被证明是不可编译的)。

++x and --x both give you back x (after it's been incremented/decremented). ++x--x 都给你回x (在它递增/递减之后)。 At that point you can do what you want with it, including assign it a new value. 此时,您可以使用它执行所需操作,包括为其指定新值。

x++ and x-- both give you back what x was (just before it was incremented/decremented). x++x--都会告诉你x是什么 (就它增加/减少之前)。 Altering this value makes no more sense than changing any ordinary function's return value: 改变这个值比改变任何普通函数的返回值没有意义:

obj->getValue() += 3; // pointless

Frankly, you should never write that. 坦率地说,你永远不应该写那个。 Postincrement and pre-increment (and decrements) should only ever be used on their own. 后增量和预增量(和减量)应该只能单独使用。 They're just recipes for confusion. 它们只是混乱的食谱。

The only place I can think of where such a situation would occur is with an operator overload of operator++ and operator=. 我能想到的唯一可能出现这种情况的地方是运算符重载运算符++和operator =。 Even then, the definition isn't clear. 即使这样,定义也不清楚。 What you code is saying basically is add one to x, then assign 5 to it. 您编码的内容基本上是将一个添加到x,然后为其分配5。 A question would arise such as why would you need to increment x before assigning 5 to it? 会出现一个问题,例如为什么在为其分配5之前需要增加x? The only possible explanation is if you have some sort of class where the ++ operator somehow increment an internal counter, then the assignment. 唯一可能的解释是,如果你有某种类,其中++运算符以某种方式递增内部计数器,然后是赋值。 No idea why such a thing is needed though. 不知道为什么需要这样的东西。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM