effective第11個條款舉的這個例子: Widget& Widget::operator=(const Widget& rhs) //一份不安全的operator=實現版本
{
delete pb;
pb = new Bitmap(*rhs.pb);
return *this;
}像上面這樣寫,自我賦值肯定會出現問題,但是為什么要先釋放掉pb呢?為什么不直接像下面這樣重賦值呢? Widget& Widget::operator=(const Widget& rhs) //一份不安全的operator=實現版本
{
pb = new Bitmap(*rhs.pb);
return *this;
}
添加回答
舉報
0/150
提交
取消