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:
&rirefers toi, so the initial value ofriis equal toi(whatever that is).- Now, when I assign the value 5 to
i, then the value ofrichanges to 5. - Now, when I change the value of
rito 10, then the reference ofritoiis removed and it now occupies a unique space with the value 10 and is no longer linked toi. - Therefore, now when I
std::cout << i and << ri, the result should display5 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?