Caprico is absolutly correct. You should not be doing this... ever.
To elaborate more on what he's saying, if you are modifying a variable, you should not read or write that variable anywhere else until the next sequence point (typically the next semicolon).
The reason for this is because the compiler has some liberties to rearrange things.
Take for instance the classic example:
1 2 3 4
|
int foo = 0;
int bar = ++foo + foo++;
cout << bar; // what is bar?
|
The prefix operator (++foo) increments foo and returns the NEW value (value after incrementing)
The postfix operator (foo++) increments foo and returns the OLD value (value before incrementing)
C++ says the above could be interpretted many ways. The compiler might choose to evaluate foo++ first, or it might choose to evaluate ++foo first.
Possible ways the compiler might interpret it are this:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
|
++foo; // prefix, foo = 1
int oldfoo = foo;
foo++; // postfix
bar = foo + oldfoo; // bar = 2
// end result: bar=2
// OR
int oldfoo = foo;
++foo; // prefix, foo = 1
bar = foo + oldfoo;
foo++; // postfix
// end result: bar=1
// OR
int oldfoo = foo;
foo++; // postfix (foo=1)
++foo; // prefix, foo = 2
bar = foo + oldfoo;
// end result: bar=3
|
None of these results are "wrong". Yet they are all different. Which you get will depend on which version of which compiler you're using. What's more, you might get something completely different! The behavior is undefined! Anything goes!
So yeah, don't do this. It's bad bad bad.
More directly, your example:
1 2
|
int bar = 1;
bar = bar--;
|
Could be interpretted several ways:
1 2 3 4 5 6 7 8 9 10
|
int oldbar = bar;
bar--; // bar=0
bar = oldbar;
// end result: bar=1
// OR
int oldbar = bar;
bar = oldbar;
bar--;
// end result: bar=0
|
Again, neither is "wrong". The behavior is undefined.