Archived

This topic is now archived and is closed to further replies.

rvalues and lvalues of class type

This topic is 5308 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

Recommended Posts

Are there any differences between rvalues of class type, and lvalues of class type? They seem to behave exactly the same.

Share on other sites
An "lvalue" can appear on the left hand side of an assignment statement.

This means, an lvalue can be something like a reference, variable, dereferenced pointer, array element etc.

A value which is not a lvalue cannot appear on the left of assignment, which would be something like:
- A constant value
- A const reference
- A dereferenced const pointer
- A computed value where the result is not itself an lvalue

It does not make sense to do

int x = 42;(x+1) = 99;

because (x+1) is not an lvalue.

Mark

Share on other sites
But an rvalue of class type can appear on the left hand side of an assignment expression.

class A{};A func(){return A();}int main(){func() = A(); //valid}

So it is the same as an lvalue in that regard.

Share on other sites
Yes it can because it really invokes the operator = on the object, which is a regular function unlike the equal sign you use on ints. If you think about it that way it makes some sense. The bigger problem is that C++ mangles the concept of rvalue/lvalues which is very strict in C. Don''t lose sleep over this. C++ is a quirky language and this is one of the examples, so just understand how it works and stop thinking about why it works.

1. 1
Rutin
43
2. 2
3. 3
4. 4
5. 5

• 9
• 27
• 20
• 9
• 20
• Forum Statistics

• Total Topics
633398
• Total Posts
3011660
• Who's Online (See full list)

There are no registered users currently online

×