Solved

Tell the difference between lvalue and rvalue.

Posted on 2001-07-05
9
643 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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
why "." vs "->" 23 119
Prototype for MessageDlg in Embacadero XE 2 2 35
C language IDE – Compilers installation 14 67
Microsoft C++ code failing in executable that worked 9 84
This article will show you some of the more useful Standard Template Library (STL) algorithms through the use of working examples.  You will learn about how these algorithms fit into the STL architecture, how they work with STL containers, and why t…
Introduction This article is a continuation of the C/C++ Visual Studio Express debugger series. Part 1 provided a quick start guide in using the debugger. Part 2 focused on additional topics in breakpoints. As your assignments become a little more …
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 be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.

919 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

16 Experts available now in Live!

Get 1:1 Help Now