Jump to content
  • Advertisement
Sign in to follow this  
_gl_coder_one_

References in C++

This topic is 4863 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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 :(

Share this post


Link to post
Share on other sites
Advertisement
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.

Share this post


Link to post
Share on other sites
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]

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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?

Share this post


Link to post
Share on other sites
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 ???

Share this post


Link to post
Share on other sites
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 )

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
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?




Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!