> Sometimes I try to compile and get an error where the 
> compiler tells me that
> my variable is not a "lvalue".  Most of the time I just mess 
> around with it
> until it goes away but I think I would be better off if I 
> fully understood
> what was going on.  Could someone please help me out?

The best way to think about an lvalue is 'an expression that could stand at
the left hand side of ann assignment'. In contrast an rvalue is 'an
expression that could stand at the left hand side of ann assignment'

E.g. 

int x = 5;

here x is an lvalue, but 5 is an rvalue, because you cannot say 

5 = x;

However, the use of the term is not restricted to assignment statements. In
C++, it is also possibl to assign to a variable using a reference. This is
most often user in function calls)

int var;

int& Foo(int x, int& z)
{
        z = x; // here you actually assign to the variable that is passed as
an argument
        return z;
}

Foo(5, var); // OK
Foo(var, 5); // ERROR, because 5 is not an lvalue; the call z = x; would be
interpreted as 5 = var;

In the above example Foo itself is an lvalue, because it is legal to say:

Foo(5, var) = 7; // results in would be that var == 7;

If Foo was declared int Foo(int x, int& z); the statement above would result
in an error.

A great source to understand all the details about lvalues, rvalues and
references this is 'the annotated C++ reference manual' by Stroustrup

Reply via email to