shows this is done in the following way
class X {
X& operator+=(const X& rhs)
{
// actual addition of rhs to *this
return *this;
}
};
inline X operator+(X lhs, const X& rhs)
{
lhs += rhs;
return lhs;
}
I understand why the argument lhs
needs to be taken by value, but why do we need to return it by value? I mean, what's the problem with the following:
inline X& operator+(X lhs, const X& rhs)
{
lhs += rhs;
return lhs;
}
Inside the function, lhs
is a new X
and modifying it does not affect any of the two operands. Since this is new, we can return it via reference, right?