Solved

Tell the difference between lvalue and rvalue.

Posted on 2001-07-05
9
652 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
Optimizing Cloud Backup for Low Bandwidth

With cloud storage prices going down a growing number of SMBs start to use it for backup storage. Unfortunately, business data volume rarely fits the average Internet speed. This article provides an overview of main Internet speed challenges and reveals backup best practices.

 
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

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

Question has a verified solution.

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

Written by John Humphreys C++ Threading and the POSIX Library This article will cover the basic information that you need to know in order to make use of the POSIX threading library available for C and C++ on UNIX and most Linux systems.   [s…
  Included as part of the C++ Standard Template Library (STL) is a collection of generic containers. Each of these containers serves a different purpose and has different pros and cons. It is often difficult to decide which container to use and …
The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.

810 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