Help | Site Map
Connecting Tech Pros Worldwide
 
 
LinkBack Thread Tools
  #1  
Old December 21st, 2007, 06:25 AM
subramanian100in@yahoo.com, India
Guest
 
Posts: n/a
Default lvalue and assignment to temporary object

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
Default 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
 

Bookmarks

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are Off
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

What is Bytes?

We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights. Get the best answers to your questions from over network members.
Post your question now . . .
It's fast and it's free

Popular Articles