Solved

Tell the difference between lvalue and rvalue.

Posted on 2001-07-05
9
637 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
 
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
6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

 
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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

In days of old, returning something by value from a function in C++ was necessarily avoided because it would, invariably, involve one or even two copies of the object being created and potentially costly calls to a copy-constructor and destructor. A…
  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 goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…
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.

760 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

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now