-2

After reading this answer I decided to try it. To my surprise, the following code is working, and the reference is correctly reseated. Why's that?

#include <iostream>

using namespace std;

int main()
{
    int a = 0;
    int b = 1;

    int& ref{a};
    ref = b;

    cout << ref << endl;

    return 0;
}

enter image description here

AmiguelS
  • 805
  • 2
  • 10
  • 28
  • 2
    Try checking the value of `a` after `ref = b;`. – G.M. Jan 18 '23 at 17:31
  • 2
    It's not doing what you think ... `ref` is still bound to `a` which will now have the value of `b`. – wohlstad Jan 18 '23 at 17:32
  • 1
    You're not reseating a reference, `ref` always refers to `a`. You've just set `a` to have the same value `b` does. – NathanOliver Jan 18 '23 at 17:32
  • 1
    It's going to work much better if you upload all pictures and photos to Facebook or Twitter, instead. Those web sites are designed for that. Stackoverflow is designed for answerable questions that get asked ***in plain text***, so that everyone can cut/paste it and try it themselves. Can you remove all images and photos from your question, and include the same information as plain text? See [ask] for more information. – Sam Varshavchik Jan 18 '23 at 17:43
  • How did you get all those pretty colors, a black background, a title bar, etc, to come out using plain text? What markdown codes were used for that? – Sam Varshavchik Jan 18 '23 at 20:30
  • @SamVarshavchik Notice how the code in that picture (which was posted to show the environment in which it was run and the output) is exactly the same as the plain text code block just before it. – AmiguelS Jan 19 '23 at 07:33
  • I'm confused. How does "the environment" have any impact on the way that references work in C++, the subject matter of this question? Would the explanation be any different if the environment's background was purple, instead of black? It is absolutely true that because everything conveyed by it "is exactly the same", it would be more appropriate to upload the picture to Facebook or Twitter, instead. Perhaps add some animation, add some mind-numbing pop song, then upload the whole thing to Youtube or Tiktok. But, as far as C++ goes, raw code, plain text, is the only thing that matters. – Sam Varshavchik Jan 19 '23 at 12:07
  • @SamVarshavchik The environment matters because it includes the C++ Standard that is used. I did not know if the behaviour of references was standard dependent. Therefore, I chose to include information about it. As you know very well, it was never about the colors of the background. If you cannot understand that then perhaps you are not such the expert you think yourself to be, judging by the presented attitude. The question was answered. I now have a better understanding of C++, and none thanks to you. You may now go on and be elitist in the question of some other poor newbie. – AmiguelS Jan 20 '23 at 13:26
  • Which specific part of the shown pictures documents "the C++ Standard", exactly? A very, very careful examination of both pictures, above, does not uncover any reference to any particular C++ standard, or offer any clue as to what specific C++ standard was used with the shown code. – Sam Varshavchik Jan 20 '23 at 13:28

2 Answers2

3

The reference ref itself was not reset. It still references the variable a. This statement

ref = b;

changed the value of the referenced variable a. To be sure insert this statement

std::cout << "a = " << a << '\n';

You can consider references as an aliases for variables they refer.

Vlad from Moscow
  • 301,070
  • 26
  • 186
  • 335
1

A reference is just an alias to the variable it refers to. Once a reference has been bound, it cannot be rebound. Anything you do to a reference afterwards actually happens instead to the variable it refers to. If you read from a reference, it reads from the variable instead. If you assign a value to a reference, it assigns the value to the variable instead. If you take the address of a reference, it takes the address of the variable instead.

So, these statements:

ref = b;
cout << ref << endl;

are really just doing this instead:

a = b;
cout << a << endl;
Remy Lebeau
  • 555,201
  • 31
  • 458
  • 770