0

I am reading this article on lvalue and rvalue. I don't quite get this code:

int x;
int& getRef ()
{
    return x;
} 
getRef() = 4;

First, how does return x give you a reference to x? Shouldn't it be something like return addressof(x)?

Second, what exactly does getRef() = 4 do? Is this suppose to be an assignment? I am at a loss here.

rts2003
  • 3
  • 4
  • `what exactly does getRef() = 4 do?`. Since `getRef()` returns a reference to `x`, `getRef() = 4` will assign `4` to `x`. – DimChtz Sep 12 '19 at 06:26
  • Something like "addressof some_thing" would be the address-of operator `&` which gives you a *pointer*. References doesn't need that. And as for `getRef() = 4;` works, think or it something like `int& temp = getRef(); temp = 4;` (it's not exactly like that but should hopefully help you understand what's happening). – Some programmer dude Sep 12 '19 at 06:27

3 Answers3

3

While references are not pointers, they usually behave much like pointers, and most code using references could be written using pointers instead. Pointers are more flexible, powerful, confusing and dangerous, so generally it's better to use references unless you actually need pointers. This same code could be written with pointers like this:

int x;
int* getPtr ()
{
    return &x; // return pointer to x
} 
*(getPtr()) = 4; // assign value to what the pointer points to

For further reading, this QA has some good stuff: When to use references vs. pointers

hyde
  • 60,639
  • 21
  • 115
  • 176
2

how does return x give you a reference to x

It does so because it's not int getRef, but int& getRef. You're right that it can be a little confusing that the return x; doesn't tell us whether a reference is returned or not. Without the & it wouldn't work and give you a compile time error at getRef() = 4;.

Second, what exactly does getRef() = 4 do? Is this suppose to be an assignment? I am at a loss here.

That's right. It assigns 4 to x. This works because the reference is an lvalue.

Blaze
  • 16,736
  • 2
  • 25
  • 44
  • Shouldn't it use something like `return referenceof(x)`? The way I understand `return x` is that it returns the value stored inside `x`. Is the new meaning of `return x` a feature of C++11? Or is it because `return x` gets combined with the `&` operator outside and gets evaluated into a pointer? – rts2003 Sep 12 '19 at 06:43
  • That's right, `return x` returns a reference because of the `&` in the function's signature. Without it, it would return a copy instead. The people who designed the language probably figured a `return referenceof(x)` wouldn't be necessary as only a reference or a value (and not both) are valid depending on what's in the function signature. – Blaze Sep 12 '19 at 06:49
  • Do you know when C++ implement this meaning of `return x`? Or has it been there since the beginning? – rts2003 Sep 12 '19 at 06:52
  • It has been there since the beginning. – Blaze Sep 12 '19 at 06:59
  • And I thought I knew it well. T_T – rts2003 Sep 12 '19 at 07:02
0

This is a rather crude example from the code qualities point of view.

The function returns the reference to the global variable x (because it is declared to return a reference and x is passed to return)

Since the Funktion Returns a reference you can directly make an assignment to the return value, because at the end it is something like a Pointer

vlad_tepesch
  • 6,681
  • 1
  • 38
  • 80