There are plenty of codes for explanation on the internet (also specifically here, on stackoverflow) that return *this
.
For example from post Copy constructor and = operator overload in C++: is a common function possible? :
MyClass& MyClass::operator=(const MyClass& other)
{
MyClass tmp(other);
swap(tmp);
return *this;
}
When I write swap as:
void MyClass::swap( MyClass &tmp )
{
// some code modifying *this i.e. copying some array of tmp into array of *this
}
Isn't enough setting return value of operator =
to void
and avoid returning *this
?