TITLE: Lvalues/rvalues with prefix/postfix ++/-- operators [ Example adapted from "C++ Gotchas", tutorial notes by Tom Cargill, page 45. ] The prefix forms of operator ++ (and --) return lvalues. The postfix forms return rvalues. #include void foo (int& i) { i *= 10; } main () { int x = 1, y = 5; foo (++x); foo (y++); printf ("x = %d y = %d\n", x, y); return 0; } The output of this program is shown below. x = 20 y = 6 RESPONSE: Jamshid Afshar, 6 Jan 94 Note that the second call to foo() is actually an error. C++ requires the initializer of a non-const reference to be an lvalue. A non-const, on the other hand, reference can be initialized by a non-lvalue. The compiler creates a variable and initializes the const reference to that variable. For example: int& r = 1; // error: a literal is not an lvalue const int& cr = 1; // translates to: const int __tmp = 1; // const int& cr = __tmp; Remember that the result of a cast (unless it's a cast to a reference) is not an lvalue, so: int i = 5; int& a = i; // okay int& b = (int)i; // error! Return values and objects created on-the-fly are not lvalues either: void move_to( TPoint& point ); TPoint origin(); void foo() { move_to( origin() ); // error move_to( TPoint(0,0) ); // error } The idea is that if you make something a non-const reference, you're intending to modify the object referenced, and the compiler won't let you silently modify a temporary object. Warning: Cfront and GNU are horribly lax and inconsistent about catching these errors.