Solved

lvalue c++ question

Posted on 2012-12-25
7
423 Views
Last Modified: 2012-12-27
If a1, a2, and a3 are of a user defined type, T, and you overload the * operator for T, then it may be possible to write:

(a1*a2) = a3;

This looks non-sensical. Yet, it compiles and runs, but I think the statement behaves like a noop.

Could you show me from the C++ standard how this can be? I thought the LHS of assignment operator is an lvalue. But a1*a2 does not look like an lvalue to me. I don't see what possible usefulness the above statement could have.
0
Comment
Question by:phoffric
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 3
7 Comments
 
LVL 86

Accepted Solution

by:
jkr earned 250 total points
ID: 38720280
>>But a1*a2 does not look like an lvalue to me.

If the overloaded operator yields a temporary instance of T that can be assigned a value, the expression would be valid yet...

>> I don't see what possible usefulness the above statement could have.

... not useful or even meaningful at all, as you wrote. Simply due to the temporary nature of the object.
0
 
LVL 32

Author Comment

by:phoffric
ID: 38720426
Thanks for your response.

From your remark, I got the impression that function returned values are temporary values which are also lvalues. Could you please show me in the C++ standard where this is and why it is necessary? Is it so that we can have statements like:   foo() = some-value; ?

There appears to be a difference between temporary values generated from User Defined Types and Built-In Types, as noted in this program:
template <class T>
class Lvalues {
public:
  Lvalues(T i) : val(i) {}
  T sumT(T arg) { (arg+val) = val; return (arg+val); }
  T val;
};

int main() {
  Lvalues<int> b1(4), b2(5), b3(6);
  Lvalues<string> a1("4"), a2("5"), a3("6");
  a1 = a2.sumT("12");  // OK, no compiler error
  cout << a1.val;
//  b1 = b2.sumT(12);
}

Open in new window

The line b1 = b2.sumT(12); has the compiler error: lvalue required as left operand of assignment. When commented out, the program gives the expected result: 125.

Why should it work for UDT, but not for Built-In-Type?
0
 
LVL 86

Assisted Solution

by:jkr
jkr earned 250 total points
ID: 38720481
Well, I don't have the standard here ATM (holiday time ;o) - so I'll try to come up with what I still have at the top of my head. Basically, the compiler is not supposed to make any assumptions about UDTs, since they could indeed be or do anything (consider someone who wants to create some plausability check of some kind that way, and don't ask, I would not try it that way either for sheer readability reasons), but on the other hand has clear rules how to handle built-in types, with such a behaviour not being desired or even meaningful. But every operation that returns an object, that object is assumed to be assignable (which I assume to be "OO-101"), thus being fit for being taken as an L-value - and that's where the conundrum starts.
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 32

Author Closing Comment

by:phoffric
ID: 38720507
Ok, thanks for the comments. I will accept that you are representing the standard reasonably well. Thanks again, and Happy Holidays to you.
0
 
LVL 86

Expert Comment

by:jkr
ID: 38720541
I hope I am (not 100% sure, but 90% should be OK - focusing on that very "temporary object"), but trying my best, and my hoilday wishs go back in your direction ;o)
0
 
LVL 86

Expert Comment

by:jkr
ID: 38725919
Loking at that after a few days, let me make one correction:

I wrote

But every operation that returns an object, that object is assumed to be assignable (which I assume to be "OO-101"), thus being fit for being taken as an L-value - and that's where the conundrum starts.
and that should have been
every operation that returns a non-const object
- maybe 'constness' could be the key to that very conundrum here?
0
 
LVL 32

Author Comment

by:phoffric
ID: 38726061
>> every operation that returns a non-const object
Right, I figured that was what you meant. Thanks for clarifying.
Happy New Year!
0

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

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

Suggested Solutions

Title # Comments Views Activity
twoTwo  challenge 35 121
operator overload: incompatible type and too many params 5 99
C++ mouse_event mouse look 7 136
Copy output image from TWindowsMediaPlayer 6 69
This article shows you how to optimize memory allocations in C++ using placement new. Applicable especially to usecases dealing with creation of large number of objects. A brief on problem: Lets take example problem for simplicity: - I have a G…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.
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.

752 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