Widget& Widget::opterator=(const Widget& rhs)
{
delete pb;
pb = new Bitmap(*rhs.pb);
return *this;
}
假设这里的rhs 就是this,delete 后 给拷贝构造函数传参,会异常。
因此,我们这样写。
Widget& Widget::opterator=(const Widget& rhs)
{
if(&rhs == this)
{
return *this;
}
delete pb;
pb = new Bitmap(*rhs.pb);
return *this;
}