0

Consider the following:

int someA = 1;
int someB = 2;

int &a = someA;
int &b = someB;

a = b;   // what happens here?

What's happening here with the references? Just curious.

Placeable
  • 589
  • 8
  • 22
  • 5
    There is no compile error here. (See e.g. http://ideone.com/RR4vcz) – Oliver Charlesworth Feb 06 '13 at 14:04
  • See http://stackoverflow.com/questions/9293674/can-we-reassign-the-reference-in-c to understand what you have done here. – KBart Feb 06 '13 at 14:05
  • @OliCharlesworth: this code is UB since it uses names reserved for implementations. :) – Yakov Galka Feb 06 '13 at 14:07
  • Also read: http://stackoverflow.com/a/228797/14065 as `_A` is reserved. – Martin York Feb 06 '13 at 14:10
  • Don't use names that begin with an underscore followed by a capital letter (`_A`) or that have two consecutive underscores. They are reserved for the implementation. – Pete Becker Feb 06 '13 at 14:11
  • @PeteBecker: Don't use identifiers beginning with an underscore. If he had used `_a` in this context it would have been just as invalid as we are in global scope. Remembering when you can and can't use '_' at the beginning is complex and often misquoted. So a simple rule is never use it. – Martin York Feb 06 '13 at 14:24
  • @LokiAstari - good point, although there is no valid context here. `` – Pete Becker Feb 06 '13 at 14:29
  • Sorry I rephrased the question since I feel this has become a bit off-topic. Sorry. – Placeable Feb 06 '13 at 14:31
  • @PeteBecker: There was before the question was re-phrased. The only way to get the compiler error was to put a statement outside a function (thus in global scope). – Martin York Feb 06 '13 at 14:31

3 Answers3

6

Having two reference variables equal to each other is in itself not an error. It may be confusing. However, what your code does is not setting a reference to another reference, it's altering the value of _A from 1 to 2, which is what's in _B.

You can ONLY set a reference ONCE [where it is initialized]. Once it's been initialized, it will simply become an alias for the original variable.

You could do this:

int &a = _A;
int &b = _A;

and

a = b;  

would store the value 1 into _A, which already has the value 1.

Mats Petersson
  • 126,704
  • 14
  • 140
  • 227
2

There is no error here, but I think I may know what you're confused about. The reference won't be reassigned, however the value of what it is referencing is reassigned.

So when you do a = b; you're essentially saying this: _A = _B, because a reference is an alias.

A reference can never be reassigned like a pointer can.

A reference can also never be null.

Tony The Lion
  • 61,704
  • 67
  • 242
  • 415
2

You can not have statements outside a function.

a = b;   //compile error?

That is a statement so must be in a function:

int _A = 1;
int _B = 2;

int &a = _A;
int &b = _B;

int main()
{
   a = b;   //compile error? No. Now it compiles.

   // As 'a' and 'b' are references.
   // This means they are just another name for an already existing variable
   // ie they are alias
   //
   // It means that it is equivalent to using the original values.
   //
   // Thus it is equivalent too:
   _A = _B;
}

Now it compiles.

Martin York
  • 257,169
  • 86
  • 333
  • 562