Effective C++ 学记之11 在operator=中处理“自我赋值“
2013-05-06 16:21
323 查看
确保当对象自我赋值时 operator= 有良好行为。其中技术包括比较“来源对象”和“目标对象”的地址,精心周到的语句顺序,以及copy-and-swap。
确定任何函数如果操作一个以上的对象,而其中多个对象是同一个对象,其行为仍然正确。
举几个潜在的自我赋值例子:
①a[i]=a[j] //i=j的时候
②*px = *py //*px 和 *py指向同一个对象
③base的引用和指针可以指向derived对象
class Base{...};
class Derived: public Base{...};
void doSomething(const Base& rb, Derived* pd); //由于base的引用和指针可以指向derived对象,rb和*pd有可能是同一个对象
④
class Bitmap{...};
class Widget{
Bitmap* pb; //指针,指向一个从堆分配而得的对象
};
Widget&
Widget::operator=(const Widget& rhs)
{
delete pb; //停止使用当前的pb
pb = new Bitmap(*rhs.pb); //使用rhs的bitmap的副本
return *this;
}
这里的自我赋值问题是:operator=函数内的*this和rhs是同一个对象的情况下,delete不只销毁了当前对象的bitmap,也销毁了rhs的bitmap。
解决:①增加证同测试
Widget::operator=(const Widget& rhs)
{
if(this == &rhs) return *this; //证同测试
delete pb;
pb = new Bitmap(*rhs.pb);
return *this;
}
以上的代码虽然解决了自我赋值的问题,但是当new Bitmap发生异常时,会导致指针指向被删除的Bitmap。
许多时候一跳精心安排的语句就可以导出异常安全:
解决:②精心安排的语句
Widget& Widget::operator=(const Widget& rhs)
{
Bitmap* pOrig = pb;
pb = new Bitmap(*rhs.pb);
delete pOrig;
return *this;
}
如上所示,如果new Bitmap抛出异常,pb仍能保持原状
解决:③
copy-and-swap技术可以保证自我赋值安全和异常安全:
class Widget{
...
void swap(Widget& rhs); //交换*this和rhs的数据;
};
Widget& Widget::operator=(const Widget& rhs)
{
Widget temp(rhs); //为rhs制作一份复件
swap(temp);
return * this
}
参数为实参的情况,rhs本身已是复件,无须在函数体内制作复件。
Widget& Widget::operator=(const Widget rhs)
{
swap(temp);
return * this
}
确定任何函数如果操作一个以上的对象,而其中多个对象是同一个对象,其行为仍然正确。
举几个潜在的自我赋值例子:
①a[i]=a[j] //i=j的时候
②*px = *py //*px 和 *py指向同一个对象
③base的引用和指针可以指向derived对象
class Base{...};
class Derived: public Base{...};
void doSomething(const Base& rb, Derived* pd); //由于base的引用和指针可以指向derived对象,rb和*pd有可能是同一个对象
④
class Bitmap{...};
class Widget{
Bitmap* pb; //指针,指向一个从堆分配而得的对象
};
Widget&
Widget::operator=(const Widget& rhs)
{
delete pb; //停止使用当前的pb
pb = new Bitmap(*rhs.pb); //使用rhs的bitmap的副本
return *this;
}
这里的自我赋值问题是:operator=函数内的*this和rhs是同一个对象的情况下,delete不只销毁了当前对象的bitmap,也销毁了rhs的bitmap。
解决:①增加证同测试
Widget::operator=(const Widget& rhs)
{
if(this == &rhs) return *this; //证同测试
delete pb;
pb = new Bitmap(*rhs.pb);
return *this;
}
以上的代码虽然解决了自我赋值的问题,但是当new Bitmap发生异常时,会导致指针指向被删除的Bitmap。
许多时候一跳精心安排的语句就可以导出异常安全:
解决:②精心安排的语句
Widget& Widget::operator=(const Widget& rhs)
{
Bitmap* pOrig = pb;
pb = new Bitmap(*rhs.pb);
delete pOrig;
return *this;
}
如上所示,如果new Bitmap抛出异常,pb仍能保持原状
解决:③
copy-and-swap技术可以保证自我赋值安全和异常安全:
class Widget{
...
void swap(Widget& rhs); //交换*this和rhs的数据;
};
Widget& Widget::operator=(const Widget& rhs)
{
Widget temp(rhs); //为rhs制作一份复件
swap(temp);
return * this
}
参数为实参的情况,rhs本身已是复件,无须在函数体内制作复件。
Widget& Widget::operator=(const Widget rhs)
{
swap(temp);
return * this
}
相关文章推荐
- Effective C++_笔记_条款11_在operator=中处理“自我赋值”
- Effective C++ -----条款11: 在operator=中处理“自我赋值”
- 【Effective c++】条款11:在operator=中处理“自我赋值”
- Effective C++ 条款10 令operator=返回一个reference to *this 条款11 在operator=中处理"自我赋值"
- Effective C++ 11 在operator=中处理“自我赋值” 笔记
- effective C++ 11_在operator= 中处理"自我赋值" 读书笔记
- Effective C++:条款11:在operator= 中处理“自我赋值”。
- Effective C++ 条款11:在operator=中处理"自我赋值"
- 在operator=中处理自我赋值(Effective C++_11)
- C++中在operator=中处理“自我赋值”(11)---《Effective C++》
- 《Effective C++》学习笔记条款11 在operator =中处理“自我赋值”
- 读书笔记《Effective c++》 条款11 在operator= 中处理“自我赋值”
- effective C++ 条款 11:在operator= 处理‘自我赋值’
- 条款11:在operator=中处理“自我赋值”
- 总结条款:11在operator=中处理“自我赋值”
- Effective C++ Item 11 在operator= 中处理“自我赋值”
- 读书笔记 effective c++ Item 11 在operator=中处理自我赋值
- 条款11:在operator=中处理“自我赋值”
- 条款11: 在operator= 中处理"自我赋值"
- Effective C++——》条款11:在operator=中处理自我赋值