1. class Widget {
    2. ...
    3. void swap(Widget& rhs);
    4. }
    5. Widget& Widget::operator=(const Widget& rhs)
    6. {
    7. Widget tmp(rhs);
    8. swap(rhs);
    9. return *this;
    10. }

    采用copy and swap技术,交互临时变量和原本变量的数据空间,临时变量在超出作用域之后会自动的执行析构函数。