References in C++
Hi
Can anyone explain the following :
int i = 10;
int &refi = i;
refi = i++;
after the above statements, i still has the value 10.
Can anyone tell the reason ... i gues i dont understands
references properly :(
edit: removed borked answer for more information see the Standard Chapter 5 paragraph 4.
[Edited by - DigitalDelusion on June 30, 2005 5:15:21 AM]
[Edited by - DigitalDelusion on June 30, 2005 5:15:21 AM]
References basically act as aliases to the variables you initialize them with.
In this case, refi is then an alias for i. As you might know, the i++ operator increments i first (to 11) and then returns the value it had before the increment (which is 10) - so the i++ in this case evaluates to 10 and you got refi = 10 there after the increment has happenend. Now refi is only an alias for i and hence you just assign 10 to i again.
I hoped that helped.
In this case, refi is then an alias for i. As you might know, the i++ operator increments i first (to 11) and then returns the value it had before the increment (which is 10) - so the i++ in this case evaluates to 10 and you got refi = 10 there after the increment has happenend. Now refi is only an alias for i and hence you just assign 10 to i again.
I hoped that helped.
No, that's not possible. Unless your compiler has a bug in it (very unlikely)
The reference points to 'i' exactly as you would assume.
But that doesn't matter because you use the ++ operator. So 'i' will always be increased regardless of refi.
[edit]Okay, a couple of people beat me, and both states that the value 10 is correct. However, since he uses i++ the increment should not occur until _after_ the assignment to refi, thus i still should take the value of 11.
(which it does in vc8)
Or is this an undefined behaivour when using post-increment and assignment in the same statement?[/edit]
The reference points to 'i' exactly as you would assume.
But that doesn't matter because you use the ++ operator. So 'i' will always be increased regardless of refi.
[edit]Okay, a couple of people beat me, and both states that the value 10 is correct. However, since he uses i++ the increment should not occur until _after_ the assignment to refi, thus i still should take the value of 11.
(which it does in vc8)
Or is this an undefined behaivour when using post-increment and assignment in the same statement?[/edit]
Order of eval of pre & post inc is implementation dependant behavior.
That code also violates the '[no] variable aliasing' rule, and this is the primary cause of grief.
MSVC 7.1 shows a value of 11.
That code also violates the '[no] variable aliasing' rule, and this is the primary cause of grief.
MSVC 7.1 shows a value of 11.
i haven't heard of that "[no] variable aliasing' rule before. Does that mean that we shouldn't alias variables in the same scope. but there is no problem for different scopes?
Quote:Original post by valla2
wait, i thought i was a well known fact that what he is doing is undefined behaviour
Y is it undefined ?
What is "[no] variable aliasing ???
the way i see it, and i could be wrong:
the statement
a = a++;
is undefined because
1) you don't know what will be evaluated first; the left part of operator = or the right part?
2) http://www.eskimo.com/~scs/C-faq/q3.2.html ( i think it's what Shannon Barber said )
the statement
a = a++;
is undefined because
1) you don't know what will be evaluated first; the left part of operator = or the right part?
2) http://www.eskimo.com/~scs/C-faq/q3.2.html ( i think it's what Shannon Barber said )
Quote:Original post by valla2
the way i see it, and i could be wrong:
the statement
a = a++;
is undefined because
1) you don't know what will be evaluated first; the left part of operator = or the right part?
2) http://www.eskimo.com/~scs/C-faq/q3.2.html ( i think it's what Shannon Barber said )
there's no undefined behavior:
B = A++; > assign the value of A to B, and then increment A
B = ++A; > increment A, and assign its (incremented) value to B
So, if we go back to the original sample:
int i = 10;
int &refi = i;
refi = i++;
what happens here is that the compiler stores the value of 'i' in a temporary register, increments 'i' , then, assigns the temporary register to 'refi', and, since refi is also 'i', we get again 10.
still not clear?
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement