I have read in many places about references:
Reference is like a const pointer
Reference always refer to an object
Once initialised, a Reference cannot be reseated
I want to make myself clear on the last point. What does that mean?
I tried this code:
#include <iostream>
int main()
{
int x = 5;
int y = 8;
int &rx = x;
std::cout<<rx<<"\n";
rx = y; //Changing the reference rx to become alias of y
std::cout<<rx<<"\n";
}
Output
5
8
Then what does it mean by "References cannot be reseated"?
This line:
Does not make rx point to y. It makes the value of x (via the reference) become the value of y. See:
Thus it is not possible to change what rx refers to after its initial assignment, but you can change the value of the thing being referenced.
A reference is therefore similar to a constant pointer (where the pointer address is the constant, not the value at that address) for the purposes of this example. However there are important differences, one good example (as pointed out by Damon) being that you can assign temporaries to local const references and the compiler will extend their lifetime to persist for the lifetime of the reference.
Considerable further detail on the differences between references and const pointers can be found in the answers to this SO post.