Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can a returned object be assignable?

In Effective C++, Item 3, Scott Meyers suggests overloading operator* for a class named Rational:

    class Rational { ... };
    const Rational operator*(const Rational& lhs, const Rational& rhs);

The reason for the return value being const-qualified is explained in the following line: if it were not const, programmers could write code such as:

    (a * b) = c;

or, more probably:

     if (a*b = c)

Fair enough. Now I’m confused as I thought that the return value of a function, here operator*, was a rvalue, therefore not assignable. I take it not being assignable because if I had:

    int foo();
    foo() += 3;

that would fail to compile with invalid lvalue in assignment. Why doesn’t that happen here? Can someone shed some light on this?

EDIT: I have seen many other threads on that very Item of Scott Meyers, but none tackled the rvalue problem I exposed here.

like image 795
qdii Avatar asked Jan 12 '12 08:01

qdii


People also ask

Can a reference from an object be returned?

Explanation: This is possible but not always, since the reference being returned may get destroyed with the return of method. This is an undesirable condition, hence it is not always possible to return references.

What does returning an object do?

It would mean make a copy and return it. The difference is that if you return pointer to objects internal variable that object state could be modified from outside. If you return copy that copy can be modified and the original object will not change.

What happens when you return an object C++?

Returning an object invokes the copy constructor while returning a reference doesn't. So, the version #2 does less work and is more efficient. The reference should be to an object that exists when the calling function is execution.

Does returning an object copy it C++?

The copy constructor is invoked when a temporary object is created as the result of a function returning an object.


1 Answers

The point is that for class types, a = b is just a shorthand to a.operator=(b), where operator= is a member function. And member functions can be called on rvalues.

Note that in C++11 you can inhibit that by making operator= lvalue-only:

class Rational
{
public:
  Rational& operator=(Rational const& other) &;
  // ...
};

The & tells the compiler that this function may not be called on rvalues.

like image 118
celtschk Avatar answered Oct 08 '22 00:10

celtschk