Solved

Tell the difference between lvalue and rvalue.

Posted on 2001-07-05
9
660 Views
Last Modified: 2008-03-17
Hi,
 I'd like to overload a function (a constructor in particular), so that it does different things depending on whether you pass it an lvalue or an rvalue. In particular, if its an lvalue, I want the object to be a deep copy of the object which it's being passed, but if its an rvalue, then I just want the object to take on the value of the argument.

e.g. superficially
class A{
int body;
int*pointer;

A(int z):body(z){}
A(int&z):pointer(&z){}

};

This probably wont work though because

int b=5;
A a(b); // will be ambiguous.

What's the correct thing to do in these circumstances?
0
Comment
Question by:glebspy
9 Comments
 
LVL 22

Expert Comment

by:nietod
ID: 6255269
You can't ovverload based on l/r.

You can overload based on const, and that is probably what you want.

For example

A:A(int &B);
A:;A(const int &B);

is legal and often useful.
0
 
LVL 22

Expert Comment

by:nietod
ID: 6255283
I suspect we need to know more about your exact needs to give you a better answer.  From what I can tell, most likely, you are going about this the wrong way.   What exactly is this class for and what exactly are you trying to do?
0
 
LVL 2

Expert Comment

by:tdubroff
ID: 6255312
I don't remember the details (I think it was with the [] operator), but in More Effective C++, Scott Meyers shows a way to distinguish between lvalues and rvalues.  I'm not sure if it would apply to a constructor, but you might want to check it out.
0
Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

 
LVL 9

Expert Comment

by:jasonclarke
ID: 6255347
> I don't remember the details

he is describing proxy classes, and how they can be used to delay determination of whether an object is being used as an lvalue or an rvalue.  I don't think it helps in this case.

I agree that this is probably the wrong approach - this makes it quite difficult to be sure that the appropriate constructor is being used in all cases.  A more explicit approach would probably be better.
0
 
LVL 1

Author Comment

by:glebspy
ID: 6256327
I can overload based on const? Wow...  isn't that weird?
Suppose I have
A(const int&);
and
A(int&);

which gets called in each of the following three cases:

void main(void){int a=4;const int b=5;

A(a);//case 1
A(b);//case 2
A(6);//case 3
}
?
0
 
LVL 22

Accepted Solution

by:
nietod earned 50 total points
ID: 6256351
case 1 is the non const version,
case 2 is the constant version
case 3 is the constant version.

In C++ unammed temporaries are constant.
0
 
LVL 22

Expert Comment

by:nietod
ID: 6256365
Think about it, the non-constant version of A can change is parameter's value and that change must be passed back to the caller.  (It may or may not make the change, the point is the fact that the parameter is a non-constant reference so it could make the change)  Thus if it were to make that change it would be chanigng a hard-coded constant.  this is conceptually illogical.   Worse it could be a problem in broader circumstances.  the point of the function might be to change the valaue of the parameter.  If the function could then be called with an unnamed temporary (which it can't) the changes would be lost, so the point of the function would be lost--most liekly by mistake.
0
 
LVL 22

Expert Comment

by:nietod
ID: 6256382
Did this actually solve the problem then?
0
 
LVL 1

Author Comment

by:glebspy
ID: 6256496
I'll have to sleep on it, but you opened up a whole new line of thought.
0

Featured Post

Free Tool: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

What is C++ STL?: STL stands for Standard Template Library and is a part of standard C++ libraries. It contains many useful data structures (containers) and algorithms, which can spare you a lot of the time. Today we will look at the STL Vector. …
C++ Properties One feature missing from standard C++ that you will find in many other Object Oriented Programming languages is something called a Property (http://www.experts-exchange.com/Programming/Languages/CPP/A_3912-Object-Properties-in-C.ht…
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.
The viewer will learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.

829 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question