0

As pointed out in various questions on stackoverflow: References are not reseatable

Immoral to reseat a reference

I am able to compile out the below code snippet :

int a1 =10;
int b = 15;

int& intref = a1;

intref = b;

reading discussion over the above cited reference, I am in a view that the above code should not compile at all. But it does. Can someone please point me out to a more explanatory resource regarding C++ references? Or give me a reason what wrong am I doing.

I am using MinGW 4.9.1 Win32 compiler, if that matters.

Community
  • 1
  • 1
Rahul Shukla
  • 1,292
  • 9
  • 25

1 Answers1

3

As you say, references are not reseatable. What's happening is that the reference (which refers to a1) is being assigned the value in b. After the assignment, both intref and a1 will be equal to b.

Mark Ransom
  • 299,747
  • 42
  • 398
  • 622
  • where I can find a code snippet, which actually gives me the error regarding "reseating" of references? – Rahul Shukla Jan 27 '15 at 05:32
  • 2
    @trivalent there's no error, because the language doesn't give you a syntax that would even let you try. The `=` sign here has two different meanings, the first one creates the reference and the second one does a value assignment. – Mark Ransom Jan 27 '15 at 05:34