C++ – Function returning variable by reference

c++

In C++,

function() = 10;

Works if function returns a variable by reference, right?

Would someone please elaborate on this in detail?

Best Solution

Consider this piece of code first

int *function();
...
*function() = 10;

Looks similar, isn't it? In this example, function returns a pointer to int, and you can use it in the above way by applying a unary * operator to it.

Now, in this particular context you can think of references as "pointers in disguise". I.e. reference is a "pointer", except that you don't need to apply the * operator to it

int &function();
...
function() = 10;

In general, it is not a very good idea to equate references to pointers, but for this particular explanation it works very well.