(Uncompiled) example with a class containing an array of ints:
class A
{
int *_ptr;
int _count;
A& operator=(const A& rhs)
{
if (this != &rhs)
{
delete _ptr;
_count = rhs._count;
_ptr = new int[_count];
std::copy(rhs._ptr, &rhs.ptr[_count], _ptr);
}
return *this;
}
};
The fact that you return a non-const reference allows for a bit awkward usage:
A a1, a2, a3;
(a1 = a2) = a3;
IMHO returning a const reference would be a better choice, even though it would not conform to the built in types.
Note how it's needed to avoid self assignment in this case. If we did not check for self-assignment, the clean-up part of the method would destroy the data before we had a chance to copy it.