Connecting Tech Pros Worldwide Help | Site Map

lvalue and assignment to temporary object

  #1  
Old December 21st, 2007, 06:25 AM
subramanian100in@yahoo.com, India
Guest
 
Posts: n/a
Consider the following:
int x;
int y;
int z;
(x+y) = z;

For this statement, I get the following error with g++ compiler:
error: non-lvalue in assignment

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);

Given this, the following are accepted by g++:
(x+y) = z;
(x+y) += z;

My question:
Is (x+y) an lvalue ?

I thought, x+y being a temporary object, we cannot take its address
and so not an lvalue. However the compiler accepts it on the left hand
side of the assignment operator. I am unable to understand this.

Kindly clarify

Thanks
V.Subramanian
  #2  
Old December 21st, 2007, 01:25 PM
James Kanze
Guest
 
Posts: n/a

re: lvalue and assignment to temporary object


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
Closed Thread


Similar Threads
Thread Thread Starter Forum Replies Last Post
comp.lang.c Answers to Frequently Asked Questions (FAQ List) Steve Summit answers 0 November 14th, 2005 04:15 AM
comp.lang.c Answers to Frequently Asked Questions (FAQ List) Steve Summit answers 0 November 13th, 2005 11:37 PM
comp.lang.c Answers to Frequently Asked Questions (FAQ List) Steve Summit answers 0 November 13th, 2005 09:56 PM
comp.lang.c Answers to Frequently Asked Questions (FAQ List) Steve Summit answers 0 November 13th, 2005 03:15 AM