as the following code shows:
class A {
public:
A(const A & rhs) {
*this = rhs;
}
A & operator=(const A & rhs) {
if (this != &rhs) {
...
...
}
return *this;
}
};
There can be issues with doing it that way if operator=() calls new and
it fails either with bad_alloc or because the object you create throws
an exception
That is why the preferred way is to get operator= to call the
copy-constructor and swap. Thus:
class A
{
public:
A( const A & ); // well defined
void swap( A& ); // well defined, possibly private
A& operator=( const A& rhs )
{
A temp( rhs );
swap( temp );
return *this;
}
};
There is no need to check here for self-assignment either. While it
would be more efficient to check for it when you are self-assigning,
99.9% of the time you are not and on those occasions you are making a
saving by not checking for it.