I don't understand the concept of postfix and prefix increment or decrement. Can anyone give a better explanation?
相关问题
- Sorting 3 numbers without branching [closed]
- How to compile C++ code in GDB?
- Why does const allow implicit conversion of refere
- thread_local variables initialization
- What uses more memory in c++? An 2 ints or 2 funct
相关文章
- Class layout in C++: Why are members sometimes ord
- How to mock methods return object with deleted cop
- Which is the best way to multiply a large and spar
- C++ default constructor does not initialize pointe
- Selecting only the first few characters in a strin
- What exactly do pointers store? (C++)
- Converting glm::lookat matrix to quaternion and ba
- What is the correct way to declare and use a FILE
All four answers so far are incorrect, in that they assert a specific order of events.
Believing that "urban legend" has led many a novice (and professional) astray, to wit, the endless stream of questions about Undefined Behavior in expressions.
So.
For the built-in C++ prefix operator,
increments
x
and produces as expression resultx
as an lvalue, whileincrements
x
and produces as expression result the original value ofx
.In particular, for
x++
there is no no time ordering implied for the increment and production of original value ofx
. The compiler is free to emit machine code that produces the original value ofx
, e.g. it might be present in some register, and that delays the increment until the end of the expression (next sequence point).Folks who incorrectly believe the increment must come first, and they are many, often conclude from that certain expressions must have well defined effect, when they actually have Undefined Behavior.
It's pretty simple. Both will increment the value of a variable. The following two lines are equal:
The difference is if you are using the value of a variable being incremented:
Here, both lines increment the value of y by one. However, the first one assigns the value of y before the increment to x, and the second one assigns the value of y after the increment to x.
So there's only a difference when the increment is also being used as an expression. The post-increment increments after returning the value. The pre-increment increments before.
'Post' means after - that is, the increment is done after the variable is read. 'Pre' means before - so the variable value is incremented first, then used in the expression.
Post increment implies the value
i
is incremented after it has been assigned tok
. However, pre increment implies the value j is incremented before it is assigned tol
.The same applies for decrement.
The difference between the postfix increment,
x++
, and the prefix increment,++x
, is precisely in how the two operators evaluate their operands. The postfix increment conceptually copies the operand in memory, increments the original operand and finally yields the value of the copy. I think this is best illustrated by implementing the operator in code:The above code will not compile because you can't re-define operators for primitive types. The compiler also can't tell here we're defining a postfix operator rather than prefix, but let's pretend this is correct and valid C++. You can see that the postfix operator indeed acts on its operand, but it returns the old value prior to the increment, so the result of the expression
x++
is the value prior to the increment.x
, however, is incremented.The prefix increment increments its operand as well, but it yields the value of the operand after the increment:
This means that the expression
++x
evaluates to the value ofx
after the increment.It's easy to think that the expression
++x
is therefore equivalent to the assignmnet(x=x+1)
. This is not precisely so, however, because an increment is an operation that can mean different things in different contexts. In the case of a simple primitive integer, indeed++x
is substitutable for(x=x+1)
. But in the case of a class-type, such as an iterator of a linked list, a prefix increment of the iterator most definitely does not mean "adding one to the object".From the C99 standard (C++ should be the same, barring strange overloading)