64,676
社区成员
发帖
与我相关
我的任务
分享
Widget& Widget::operator=(const Widget& rhs)
{
if (this != &rhs)//这里应该先判断下,否则若赋值对象和被赋值对象是同一个对象时下面的delete pb;和pb = new Bitmap(*rhs.pb);可能会引起错误
{
delete pb;//删除当前pb指向的Bitmap对象,否则下面的pb被赋值后会引起内存泄露
pb = new Bitmap(*rhs.pb);//pb指向新动态申请的Bitmap对象,并用*rhs.pb作参数调用Bitmap的拷贝构造函数来初始化
}
return *this;
}