Although the question might not be too precise, I think I still got the point:
Imagine, the code would have been written as follows:
Widget& Widget::operator=(const Widget& rhs)
{
if (rhs == *this) // actually, you'd rather do &rhs == this!
// you don't want self-assignment
return;
delete pb;
pb = new Bitmap(*rhs.pb);
return *this;
}
What happens, if new Bitamp()
fails with an exception – then pb is already deleted – and points to invalid memory!
So you remember first the value of pb, and if creation fails with an exception, you have not modified this
and it remains valid even if an exception occured.
solved why does overload operator= makes exception safety