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;
}