Click here to Skip to main content
15,886,963 members
Home / Discussions / C / C++ / MFC
   

C / C++ / MFC

 
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 
No, the writer would no have used the word statement. The full quote I gave in an earlier post read:

"The difference is important, however, when these operators are used in statements that do more than just incrementing or decrementing. For example, look at the following lines:

num = 4;
cout << num++;

The cout statement above is doing two things: (1) displaying the value of the num, and (2) incrementing the num. But which happens first? cout will display a different value if num is incremented first than if num is incremented last. The answer depends upon the mode of the increment operator.
Postfix mode causes the increment to happen after the value of the variable is used in the expression. In the statement above, cout will display 4 and then num will be incremented to 5."

I notice the writer does use the word statement a couple of times in that quote. The writer then goes on to explain how the postfix operator works the same in mathematical expressions. On the next page he writes, "The increment and decrement operators can also be used on variables in mathematical expressions. Consider the following program segment:

a=2;<br />
b=5;<br />
c=a*b++;<br />
cout << a << " " << b << " " << c;


In the statement c = a * b++ , c is assigned the value of a times b , which is 10. b is then incremented. The cout statement will display:

2 6 10 "

But of course this doesn't cover the case where b , for example, is used twice in the same expression. The instruction leads one to believe that in a statement, for example, such as b * b++ , that as in the above b++ would be incremented after used in the expression b * b . This is how it works on my compiler. Apparently that is not so, but you can see where this instruction, and others, would lead one to believe what I had previously assumed.


pesho2932 wrote:
And if you find funny what I say about the order of evaluation of operands, you should revise your knowledge of sequence points.

My, someone's getting touchy. By the way, weren't you the one that asked the question (well, more of a statment really) that started this thread? And now you say you know all about it?

As to your original question. You posted:

int x = 1;<br />
x = x++;


And then stated:

"I think this should be well defined.
The operations are:
1. ++
2. =
As the expression x++ is an operand of operator=, I think it should be evaluated first (??).
The final value of x should then be 1."

In your original post the question is trivial. It does not matter when x++ executed, since the increment also reassigns the incremented variable back to itself, as you noted, "But the increment seems to occur after the assignment. And x = 2." So who's the one that should revise their knowledge of C++?"

Danny
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 
QuestionVC++6.0 - Finding users privileges Pin
Rainos15-Sep-05 4:48
Rainos15-Sep-05 4:48 
AnswerRe: VC++6.0 - Finding users privileges Pin
David Crow15-Sep-05 7:32
David Crow15-Sep-05 7:32 
GeneralRe: VC++6.0 - Finding users privileges Pin
Rainos15-Sep-05 21:49
Rainos15-Sep-05 21:49 
GeneralRe: VC++6.0 - Finding users privileges Pin
David Crow16-Sep-05 2:11
David Crow16-Sep-05 2:11 
GeneralRe: VC++6.0 - Finding users privileges Pin
Rainos16-Sep-05 2:49
Rainos16-Sep-05 2:49 
GeneralRe: VC++6.0 - Finding users privileges Pin
David Crow16-Sep-05 4:59
David Crow16-Sep-05 4:59 
GeneralRe: VC++6.0 - Finding users privileges Pin
Rainos16-Sep-05 5:42
Rainos16-Sep-05 5:42 
GeneralRe: VC++6.0 - Finding users privileges Pin
David Crow16-Sep-05 5:48
David Crow16-Sep-05 5:48 
QuestionCTabCtrl Background in XP Themed Pin
RickyC15-Sep-05 4:15
RickyC15-Sep-05 4:15 
AnswerRe: CTabCtrl Background in XP Themed Pin
Ravi Bhavnani15-Sep-05 8:07
professionalRavi Bhavnani15-Sep-05 8:07 
GeneralRe: CTabCtrl Background in XP Themed Pin
RickyC15-Sep-05 9:39
RickyC15-Sep-05 9:39 
GeneralRe: CTabCtrl Background in XP Themed Pin
Ravi Bhavnani15-Sep-05 9:44
professionalRavi Bhavnani15-Sep-05 9:44 

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.