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