1

I am new to C++. With the following minimal program:

#include <iostream>

int main()
{
    int i, &ri =i;
    i = 5; ri = 10;
    std::cout << i  << " " << ri << std::endl;
}

I observe the output to be

10 10

where I expected it to be

5 10

My line of reasonning is:

  1. &ri refers to i, so the initial value of ri is equal to i (whatever that is).
  2. Now, when I assign the value 5 to i, then the value of ri changes to 5.
  3. Now, when I change the value of ri to 10, then the reference of ri to i is removed and it now occupies a unique space with the value 10 and is no longer linked to i.
  4. Therefore, now when I std::cout << i and << ri, the result should display 5 10.

Instead it seems that i is referring to ri (I checked by changing the values that I assign to ri).

Can you please let me know what I am thinking incorrectly?

YSC
  • 38,212
  • 9
  • 96
  • 149
  • 6
    Assigning to `ri` changes _what `ri` refers to_, not `ri` itself. That's what references are for. – Paul Sanders Nov 07 '18 at 16:32
  • 4
    #3 is where you are wrong. ***then the reference of ri to i is removed*** No it still is a reference. – drescherjm Nov 07 '18 at 16:32
  • 4
    Sounds like you could use a [good C++ book](http://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list) – NathanOliver Nov 07 '18 at 16:33
  • 3
    Please, avoid this syntax `int i, &ri =i;` and instead separate each declaration on its own line. – DeiDei Nov 07 '18 at 16:34
  • 3
    Even though this is a question about a basic aspect of C++, I think it is an on-topic question. The OP provided an MCVE, the observed behavior and the expected behavior. I've seen far worse. +1 – YSC Nov 07 '18 at 16:49
  • 1
    Think of references as aliases for the variable they refer to. A reference *is* the same as the referenced variable. It's just another name for it. – Jesper Juhl Nov 07 '18 at 17:04
  • 1
    @JesperJuhl Thanks! This is possibly the most succinct and clear explanation I could get. Thanks! –  Nov 07 '18 at 17:18

4 Answers4

3

C++ is not java. You cannot reassign a reference.

ri = 10; does not create another integer for ri to bind to. It is actually changing the value of what ri is bound to, which is i.

As suggested in the comments, you really should go for a good C++ book.

YSC
  • 38,212
  • 9
  • 96
  • 149
  • 1
    Thanks - this is clear. Actually, someone referred me to the link you mentioned and I picked up C++ Primer. I mistakenly left out a couple of lines where this was explained. It was basically my carelessness. –  Nov 07 '18 at 16:43
1

r and ri simply are two names for the same thing. You first assign 5 to that thing and later 10. After that, of course that one thing is 10 and referring to it with any of the names will give you that common value.

cdonat
  • 2,748
  • 16
  • 24
0

Reference means that you are assigning a variable &i to have the same memory address as the variable you are setting it equal to -- i. Thus, changing something about the information at that address changes any variable that is looking at that address for it's value (i.e. a reference or pointer variable).

Little Boy Blue
  • 311
  • 4
  • 17
0

ri is a reference to i. References work like pointers. ri and i are the same memory. If you type

std::cout << &i << "  " << &ri << std::endl;

both will be the same. Setting ri to 10 doesn't give it new memory but sets current.