Chinaunix首页 | 论坛 | 博客
  • 博客访问: 43929
  • 博文数量: 25
  • 博客积分: 930
  • 博客等级: 准尉
  • 技术积分: 257
  • 用 户 组: 普通用户
  • 注册时间: 2008-12-25 08:59
文章分类

全部博文(25)

文章存档

2010年(6)

2009年(18)

2008年(1)

我的朋友
最近访客

分类: C/C++

2009-04-21 10:20:22

条款11:在operator=中处理 ”自我赋值“
a[i]=a[j]; 
*p=*q; 
都潜在自我赋值。在类的继承中也有可能出现。 
void dosomething(const Base& rb,Derived * pd); 
rb和*pd有可能是一个对象 
class Bitmap {....}; 
class Widget 
{ 
private: Bitmap * pb; 
};
 Widget& Widget::operator=(const Widget& rhs) 
{  if(*this=rhs) return *this; //处理代码 
   delete pb;
   pb=new Bitmap(*rhs.pb); 
   return *this; 
}
 Widget& Widget::operator=(const Widget& rhs) 
{   Bitmap *tmp=new Bitmap(*rhs.pb);
   delete pb; pb=tmp;
   return *this; } 
Widget& Widget::operator=(const Widget& rhs) 
{   Bitmap *tmp=pb; 

   pb=new Bitmap(*rhs.pb);

   delete tmp;

   return *this;

}

阅读(487) | 评论(0) | 转发(0) |
0

上一篇:Effective C++ 10.1

下一篇:C++笔记

给主人留下些什么吧!~~