1

In The C++ programming language, it said "a reference cannot be made to refer to a different object after its initialization." However, when I wrote the code below, it printed 20, which means that the reference refer to another object.

int a = 10; int b = 20; int& ref = a; ref = b; cout << ref << endl; 

However, when I wrote the code below, it printed 10, the vaule of a.

int a; int b; int& ref = a; ref = b; a = 10; b = 20; cout << ref << endl; 

Why does this happen? How should I understand C++ reference?

3
  • 5
    try printing a at the end of your first case Commented Apr 20, 2021 at 7:06
  • 1
    "How should I understand C++ reference?" : You cannot modify what a reference points at. Initializing a reference type is not the same as assigning to one. Commented Apr 20, 2021 at 7:09
  • After int& ref = a;, ref means exactly the same thing as a. The first is equivalent to int a = 10; int b = 20; a = b;, the second to int a; int b; a = b; a = 10; b = 20;. Commented Apr 20, 2021 at 7:35

4 Answers 4

5

In the line

int& ref = a; 

You set ref to be a reference to the variable a. You then set the value of this object to be the value of b in the next line:

ref = b; 

Note that a will now have the value of b (which since you haven't initialised it yet will be zero).

Next, you set the value of a to be 10

a = 10; 

At this point ref will also be set to be 10, since it is a reference to a. You then set the value of b to be 20. But this makes no difference to ref as it is it is a reference to a, which is still 10.

So, when you print out ref, you get the value of a - i.e. 10.

Sign up to request clarification or add additional context in comments.

2 Comments

(which since you haven't initialized it yet will be zero) Don't count on that. It only MIGHT be zero if a is an automatic variable local to a function, and the cout << ref << endl; suggests a function is involved.
True. Also depends on the compiler you are using.
3

ref = b does not mean "ref should now reference b". It means "Copy the value of b to the variable referenced by ref".

So this code:

int& ref = a ref = b; a = 10; 

Is equivalent to

a = b; a = 10; 

Comments

1

A reference is just alias. It does not have separate memory. So when you write:

int a = 10; int b = 20; int& ref = a; //ref has same address a a. Its just alias ref = b; //value of b assigned to ref, ref still points to same address cout << ref << endl; 

It says that ref is alias for a. So same address as a.

When you write ref = b, you are assigning the value of b to ref, in turn to a.

So, when you print ref, a and b will both print the same value i.e 20, but their addresses are not the same. a and b still take up different addresses.

In the second example of yours, ref is alias for a. Then you assign a value of b to ref. Still ref has the address of a. Then you changed the value of b. So b gets a new value but ref is still pointing to the address of a, which does not get modified.

Hope this clears up your confusion.

To understand better, try printing the addresses and values of variables. That way, you will get better understanding.

1 Comment

A reference does not have a memory address of its own. It might not even be implemented using a variable at all. Once a reference is bound to something, querying the reference for its address will return the address of the thing it refers to instead. Just like assigning a value to a reference will assign to the thing it refers to. And reading a value from a reference will read from the thing it refers to.
0

I made a small program that tries to illustrate the difference between references and pointers. Hopefully this will help you see how the "cannot be made to refer to a different object" works:

int main() { int a = 10; int b = 20; int* p = &a; std::cout << a << ", " << b << ", " << *p << ". p points to a\n"; *p = 11; std::cout << a << ", " << b << ", " << *p << ". Changing p changes a\n"; a = 12; std::cout << a << ", " << b << ", " << *p << ". Changing a changes p\n"; p = &b; std::cout << a << ", " << b << ", " << *p << ". p points to b\n"; *p = 21; std::cout << a << ", " << b << ", " << *p << ". Changing p now changes b, but a is unchanged as p no longer points to it\n"; b = 22; std::cout << a << ", " << b << ", " << *p << ". Changing b changes p\n"; int& r = a; std::cout << a << ", " << b << ", " << r << ". r references a\n"; r = 13; std::cout << a << ", " << b << ", " << r << ". Changing r changes a\n"; a = 14; std::cout << a << ", " << b << ", " << r << ". Changing a changes r\n"; r = b; std::cout << a << ", " << b << ", " << r << ". The value of r is set to the value of b, but r still references a and thus a changes too\n"; b = 23; std::cout << a << ", " << b << ", " << r << ". Changing b does NOT change r\n"; r = 24; std::cout << a << ", " << b << ", " << r << ". Changing r only changes a\n"; } 
10, 20, 10. p points to a 11, 20, 11. Changing p changes a 12, 20, 12. Changing a changes p 12, 20, 20. p points to b 12, 21, 21. Changing p now changes b, but a is unchanged as p no longer points to it 12, 22, 22. Changing b changes p 12, 22, 12. r references a 13, 22, 13. Changing r changes a 14, 22, 14. Changing a changes r 22, 22, 22. The value of r is set to the value of b, but r still references a and thus a changes too 22, 23, 22. Changing b does NOT change r 24, 23, 24. Changing r only changes a 

The point here is that with the pointer you can do p = &b and refer to a different object. This is impossible with a reference.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.