On Dec 21, 7:18 am, "subramanian10...@yahoo.com, India"
<subramanian10...@yahoo.comwrote:
Quote:
Consider the following:
int x;
int y;
int z;
(x+y) = z;
|
Quote:
For this statement, I get the following error with g++ compiler:
error: non-lvalue in assignment
|
Correct.
Quote:
Suppose I have a class Test and x, y, z are objects of type Test.
Suppose I have
Test Test::operator+(const Test& ref);
Test operator+=(Test lhs, Test rhs);
|
Quote:
Given this, the following are accepted by g++:
(x+y) = z;
(x+y) += z;
|
Quote:
My question:
Is (x+y) an lvalue ?
|
No. But the = here isn't really an operator, either; it's more
a particular syntax of a function call. (Overloaded "operators"
are only partially operators---they have the syntax of
operators, but semantics of a function call.)
Since you can call a member function on an lvalue, the above is
legal.
Quote:
I thought, x+y being a temporary object, we cannot take its
address and so not an lvalue.
|
That would be too simple. x+y is a temporary object (an
rvalue), and *you* cannot take its address. rvalues of class
types do have addresses, however, and the compiler can get at
the address if it needs to, for example to call a member
function.
(Technically, you cannot take its address is only partially
true. You can not apply the built-in operator & on it, since
the built-in operator & requires an lvalue, which it is not. If
the class overloads operator&, however, we're back to the above
rule: the overloaded operator is a function call of a member
function, which is legal.)
Quote:
However the compiler accepts it on the left hand side of the
assignment operator. I am unable to understand this.
|
It is confusing. There are several different concepts involved
simultaneously, and there is also the fact that rvalue means
something different for class types than for non-class types.
--
James Kanze (GABI Software) email:james.kanze@gmail.com
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34