operator=处理自我赋值


有许多时候,我们自己编写类的operator=函数(例如,当类中包含指针时)。
考虑如下的一个类:
class Widget {
public:
  Widget(int x=0): val(new int(x)) {}
  ~Widget() { delete val; }
  Widget(const Widget &rhs): val(new int(*rhs.val)) {}
  //operator =
  Widget& operator=(const Widget &rhs);

  void print() const { cout << *val << endl; }
private:
  int *val;
};

错误版本:

当自我赋值的时候,下面的代码是不安全的。如果*this 和 rhs 是同一个对象,那么第一句 delete val;
不仅仅删除了当前对象的val, 也删除了rhs 的 val. 那么 rhs.val 指向的就是一个已经被删除的int. 这必然产生诡异的问题。
/** wrong version
 * not-safe when self-assignment occurs.
 */
Widget& Widget::operator=(const Widget &rhs) {
  delete val;
  val = new int(*rhs.val);
  return *this;
}

改进版本1:

阻止此类错误的传统做法是加一个证同测试(identity test)。
Widget& Widget::operator=(const Widget &rhs) {
  if(this == &rhs) return;

  delete val;
  val = new int(*rhs.val);
  return *this;
}

但是上述做法,虽然具有自我复制安全性,但是不具有异常安全性。
如果 new int 抛出异常的时候,this->val 指向的就是一个已经删除的int

改进版本2:

异常安全性与自我复制安全性兼具
Widget& Widget::operator=(const Widget &rhs) {
  int *pOld = val;
  val = new int(*rhs.val);
  delete pOld;
  return *this;
}

改进版本3:

copy and swap 技术
Widget& Widget::operator=(const Widget &rhs) {
  Widget temp(rhs);
  swap(temp);
  return *this;
}

改进版本4:

通过传值来实现 copy and swap
Widget& Widget::operator=(Widget rhs) { //yes, copy by value
  swap(rhs);
  return *this;
}
  • 4
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值