Click here to Skip to main content
16,005,120 members
Home / Discussions / C / C++ / MFC
   

C / C++ / MFC

 
GeneralRe: what about x = x++; ? Pin
bugDanny15-Sep-05 7:23
bugDanny15-Sep-05 7:23 
GeneralRe: what about x = x++; ? Pin
Joaquín M López Muñoz16-Sep-05 5:00
Joaquín M López Muñoz16-Sep-05 5:00 
GeneralRe: what about x = x++; ? Pin
bugDanny16-Sep-05 6:45
bugDanny16-Sep-05 6:45 
GeneralRe: what about x = x++; ? Pin
Joaquín M López Muñoz16-Sep-05 7:05
Joaquín M López Muñoz16-Sep-05 7:05 
GeneralRe: what about x = x++; ? Pin
bugDanny16-Sep-05 7:41
bugDanny16-Sep-05 7:41 
AnswerRe: what about x = x++; ? Pin
Anonymous15-Sep-05 8:30
Anonymous15-Sep-05 8:30 
GeneralRe: what about x = x++; ? Pin
Anonymous15-Sep-05 8:38
Anonymous15-Sep-05 8:38 
GeneralRe: what about x = x++; ? Pin
bugDanny15-Sep-05 8:57
bugDanny15-Sep-05 8:57 
Anonymous wrote:
x = x++;

in this case x++ must be evaluated before it is passed as an argument to the assignment operator.
so i thought the side effect of x++ (incrementing x) must also be complete before the assignment takes place. But maybe it is not so.


It is not so. First, the x part of x++ is passed to the operator=() function, then x++ increments x. When x++ increments x, the incremented number is at the same time reassigned to x. Try,

printf("%d", x++);

This might show it better, since now it is clearly seen that x++ is being passed to printf. That line will print the original value of x, then increment x.

Anonymous wrote:
Maybe because u know the result of x++ without having to increment u can postpone it for the end of the whole statement for example.

This is sort of correct. You don't know the result of x++ yet. First, x is passed into the other expression in the line, then x is incremented. Take your example:

Anonymous wrote:
y = ((x) + (x++))

This line is made up of 3 expressions: y=, x++, and x + x++. The = expression has to wait until an expression is ran in order to pass something to y=. So you say,

Anonymous wrote:
but u don't know whether (x) or (x++) will be evaluated first.

But you do know. Let's rewrite the line using functions instead of operators. This line would be:

y.operator=(x.operator+(x++));

We know C++ works from the inside out. y.operator=() is called, but that needs to evaluate x.operator+(x++)) before it can execute, so x.oeprator+() is called, but that needs to evaluate x++ before it can execute. So first x++ is examined and passed to x.operator+(). This means that the original x is passed to the function before it is incremented, since we are using the postfix of ++. The x.operator+() was called with the original x; it was called before x was incremented.

So x is passed to x.operator+() , so we have x + x , then x is incremented, which doesn't affect the original y.operator=() since the expression x + x was passed to that.

As I have pointed out in other responses, we should avoid confusion in our code. Here is that same line of code written out to avoid confusion. This is how that line works:

int x = somevalue;<br />
int y;<br />
int temporaryValue = x + x;<br />
x++;<br />
y = temporaryValue;


Hope this unconfuses anything.

Danny
GeneralRe: what about x = x++; ? Pin
Chris Losinger15-Sep-05 8:59
professionalChris Losinger15-Sep-05 8:59 
GeneralRe: what about x = x++; ? Pin
bugDanny15-Sep-05 9:05
bugDanny15-Sep-05 9:05 
GeneralRe: what about x = x++; ? Pin
David Crow15-Sep-05 9:32
David Crow15-Sep-05 9:32 
GeneralRe: what about x = x++; ? Pin
pesho293215-Sep-05 21:02
pesho293215-Sep-05 21:02 
GeneralRe: what about x = x++; ? Pin
David Crow16-Sep-05 2:15
David Crow16-Sep-05 2:15 
GeneralRe: what about x = x++; ? Pin
bugDanny16-Sep-05 3:15
bugDanny16-Sep-05 3:15 
GeneralRe: what about x = x++; ? Pin
pesho293216-Sep-05 3:36
pesho293216-Sep-05 3:36 
GeneralRe: what about x = x++; ? Pin
bugDanny16-Sep-05 4:19
bugDanny16-Sep-05 4:19 
GeneralRe: what about x = x++; ? Pin
pesho293218-Sep-05 21:48
pesho293218-Sep-05 21:48 
GeneralRe: what about x = x++; ? Pin
bugDanny19-Sep-05 3:02
bugDanny19-Sep-05 3:02 
GeneralRe: what about x = x++; ? Pin
pesho293219-Sep-05 5:38
pesho293219-Sep-05 5:38 
GeneralRe: what about x = x++; ? Pin
bugDanny19-Sep-05 7:43
bugDanny19-Sep-05 7:43 
GeneralRe: what about x = x++; ? Pin
pesho293219-Sep-05 21:14
pesho293219-Sep-05 21:14 
GeneralRe: what about x = x++; ? Pin
bugDanny20-Sep-05 3:16
bugDanny20-Sep-05 3:16 
GeneralRe: what about x = x++; ? Pin
pesho293220-Sep-05 4:52
pesho293220-Sep-05 4:52 
GeneralRe: what about x = x++; ? [Modified 2005-09-19] Pin
Maxwell Chen15-Sep-05 21:17
Maxwell Chen15-Sep-05 21:17 
GeneralRe: what about x = x++; ? Pin
bugDanny16-Sep-05 3:21
bugDanny16-Sep-05 3:21 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.