Search code examples
c++referencelvalue

C++ does reference return a lvalue or rvalue?


int a, b;
a=10;
&b=a;

This simple code causes the following error when compiled.

error: lvalue required as left operand of assignment

But &b is indeed on the left hand-side. So what does the error message exactly mean?


Solution

  • In this context & is the address-of operator. The value of &b is a pointer of type int *. It is a prvalue. This is unrelated to use of the & symbol in a declaration (where it means the declaration of a reference) -- symbols mean different things in declarators than they do in expressions.

    You cannot change the address of a variable. Variables have the same address for their whole lifetime.