The member function implicitly takes this
as a parameter,
So take operator+
as a member function, should only have one explicit parameter:
void Point::operator+(const Point& rhs)
{
...
}
And if take operator+
as a non-member function, sholud have two parameters:
Point operator+(Point& lhs, const Point& rhs)
{
...
}
And for std::move
, it casts rvalue reference to rvalue, and move lhs into return value, only reasonable to use when lhs passed as rvalue reference:
Point operator+(Point&& lhs, const Point& rhs)
{
lhs += rhs;
return std::move(lhs);
}
Or you may consider use std::forward, if pass lhs as lvalue reference and rvalue reference both exits:
template<
typename T,
typename = typename std::enable_if<
std::is_same<Point, typename std::decay<T>::type>::value
>::type>
Point
operator+(T&& lhs, const Point& rhs)
{
lhs += rhs;
return std::forward<T>(lhs); // move rvalue into return value, copy lvalue
}
Refer to Effective Modern C++ by Scott Meyers item 25: Use std::move
on rvalue references, std::forward
on universal references.
p1+=p2; return p1;
now, but I would also need to do the same withmove
– Cavill