6

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"?

Community
  • 1
  • 1
cppcoder
  • 22,227
  • 6
  • 56
  • 81
  • Do not take seriously assertion that says `Reference is like a const pointer`. It is wrong. Then in `main()` do `std::cout< – Keynslug Jul 10 '12 at 08:51
  • Reference **is** like a constant pointer. It is **not** like a pointer to constant. See article [here](http://www.codeguru.com/cpp/cpp/cpp_mfc/general/article.php/c6967/Constant-Pointers-and-Pointers-to-Constants.htm) for the details. – Alex Wilson Jul 10 '12 at 08:55
  • 1
    @AlexWilson I prefer to not confuse cppcoder to think that references are somehow like constant pointers. Generally it is ok to say "reference **behaves** like constant pointer in terms of access to referenced object". But they are different entities in many ways. – Keynslug Jul 10 '12 at 09:10
  • @Keynslug. Fair enough, there are differences as outlined [here](http://stackoverflow.com/questions/2336230/difference-between-const-pointer-and-reference). But after the immediate front end pass, most compilers will not differentiate between references and constant pointers internally. At least one compiler of my acquaintance decays references and their accesses to constant pointers (and constant pointer accesses) almost immediately in their internal representation. – Alex Wilson Jul 10 '12 at 09:15
  • @AlexWilson Yes, and I'm sure it is, just because reference implemetation is not specified by any C++ standard, and way you mentioned is the most natural way of internal reference implementation. – Keynslug Jul 10 '12 at 09:31
  • 3
    @AlexWilson: In general, it does not matter what a compiler of your acquaintance does or what you think it does, your statement (as well as below answer) is still wrong. One easy example why your assumption is wrong is temporary object lifetime extension. This won't happen with a const pointer, ever. If references decayed to pointers, your compiler would consequently be unable to comply with a guarantee given by the standard. – Damon Jul 10 '12 at 09:32
  • 1
    @Damon. Now that is a good point. My apologies to all. I will update as appropriate. – Alex Wilson Jul 10 '12 at 09:33

3 Answers3

13

This line:

rx = y;

Does not make rx point to y. It makes the value of x (via the reference) become the value of y. See:

#include <iostream>

int main()
{
    int x = 5;
    int y = 8;

    int &rx = x;
    std::cout << rx <<"\n";

    // Updating the variable referenced by rx (x) to equal y
    rx = y;    
    std::cout << rx <<"\n";
    std::cout << x << "\n";
    std::cout << y << "\n";
}

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.

Community
  • 1
  • 1
Alex Wilson
  • 6,690
  • 27
  • 44
  • 1
    So, by the statement, "References cannot be reseated", it means the address does not change, but the value can change. – cppcoder Jul 10 '12 at 08:55
  • Yes. It does mean that. So in [this article](http://www.codeguru.com/cpp/cpp/cpp_mfc/general/article.php/c6967/Constant-Pointers-and-Pointers-to-Constants.htm) (which I also mentioned in a comment on your question) it is like a constant pointer. – Alex Wilson Jul 10 '12 at 08:56
6

You changed the value of x to the value of y :-)

Torsten Robitzki
  • 3,041
  • 1
  • 21
  • 35
6

int &rx = x; makes rx an alias to x.

Then, rx = y implies x = y

After an alias is made, any use of it (rx) will be equivalent to use of x. This you cannot undo (reseat rx) to make rx an alias of say 'y'.

Chethan
  • 905
  • 1
  • 10
  • 18