构造/析构/赋值 函数

条款10:令operator=返回一个reference to *this

赋值操作符运算是由右向左运算的。例如一个连锁赋值
[cpp]  view plain  copy
 
  1. <span style="font-size:14px;">int x, y, z;  
  2. x=y=z=15;</span>  

编译器解释时时这样的:
x=(y=(z=15));
先给z赋值,用赋值后的z再给y赋值,用赋值后的y再给x赋值。
为了实现连锁赋值,操作符必须返回一个reference指向操作符左侧的实参。
其实,如果operator=不返回一个引用,返回一个临时对象,照样可以实现连锁赋值。但这个临时对象的构建会调用拷贝构造函数。看下面这个例子:
[cpp]  view plain  copy
 
  1. #include<iostream>  
  2. using namespace std;  
  3.   
  4. class Widget  
  5. {  
  6.   
  7. public:  
  8.     Widget()  
  9.     {  
  10.         cout<<"Default Ctor"<<endl;  
  11.     }  
  12.     Widget(const Widget& rhs)  
  13.     {  
  14.         cout<<"Copy Ctor"<<endl;  
  15.     }  
  16.     Widget& operator=(const Widget& rhs)  
  17.     {  
  18.         cout<<"operator="<<endl;  
  19.         return *this;  
  20.     }  
  21. };  
  22. int main()  
  23. {  
  24.     Widget a,b,c;  
  25.     a=b=c;  
  26.     return 0;  
  27. }  
这样输出为:
Default Ctor
Default Ctor
Default Ctor
operator=
operator=
如果把operator=返回的引用去掉,改为Widget operator=(const Widget& rhs)
则会输出:
Default Ctor
Default Ctor
Default Ctor
operator=
Copy Ctor
operator=
Copy Ctor
返回临时对象,临时对象再给左侧变量赋值。多出了一步,浪费资源。

operator=是改变左侧操作数的,与其类似的operator+=、operator-=等改变左侧操作符的运算,都应该返回引用。这是一个协议,应该去遵守。

条款11:在operator=中实现“自我赋值”

自我赋值是指对象给自己赋值。
Widget w;
w=w;
这样看起来有点愚蠢,但是它合法。上面这个例子很容易发现自我赋值。但有时候就不那么容易了,例如:数组a
a[i]=a[j];
当i和j相等时,就是自我赋值。
例如,两个指针px和py
*px=*py;
如果两个指针指向同一个对象,这也是自我赋值。
除此之外,还有引用。更加隐晦的自我赋值发生在基类和派生类层次中,不同类型的指针或引用之间的赋值都有可能发生自我赋值。

如果遵循条款13和条款14,运用对象来管理资源,确定“资源管理对象”在copy发生时有正确的举措,这样自我赋值是安全的。如果自己管理资源,可能会“在停止使用资源之前意外释放了它”。
例如使用一个class管理一个指针。
[cpp]  view plain  copy
 
  1. class Widget  
  2. {  
  3. public:  
  4.     Widget& operator=(const Widget& rhs)  
  5.     {  
  6.         delete p;  
  7.         p=new int(ths.p);  
  8.         return *this;  
  9.     }  
  10.     int *p;  
  11. };  
如果上面代码自我赋值,在使用指针p之前已经将其释放掉了。
防止这种问题发生的办法是“证同测试”,在删除前判断是不是自我赋值
[cpp]  view plain  copy
 
  1. class Widget  
  2. {  
  3. public:  
  4.     Widget& operator=(const Widget& rhs)  
  5.     {  
  6.         if(this==&rhs)//证同测试  
  7.             return *this;  
  8.         delete p;  
  9.         p=new int(rhs.p);  
  10.         return *this;  
  11.     }  
  12.     int *p;  
  13. };  
这个版本的operator=可以解决自我赋值的问题。但是还有个问题:异常安全。如果delete p成功,而p=new int(rhs.)失败会发生什么?
这时,widget对象会持有一个指针,这个指针指向了被释放的内存。下面方法可以实现异常安全。
[cpp]  view plain  copy
 
  1. class Widget  
  2. {  
  3. public:  
  4.     Widget& operator=(const Widget& rhs)  
  5.     {  
  6.         int tmp=p;//记录原先内存  
  7.         p=new int(rhs.p);  
  8.         delete tmp;//释放原先内存  
  9.         return *this;  
  10.     }  
  11.     int *p;  
  12. };  
在实现异常安全的同时,其实也获取了自我赋值的安全。如果p=new int(ths.p)发生异常,后面的delete tmp就不会执行。
如果你很关心效率,可以把“证同测试”放到函数起始处。但是“自我赋值”发生的频率有多高?因为“证同测试”也需要成本,因为它加入了新的控制分支。
还有一个替代方案是:copy and swap技术。这个技术和异常安全关系密切,条款29详细说明。下面看它怎么实现
[cpp]  view plain  copy
 
  1. class Widget  
  2. {  
  3. public:  
  4.     void swap(const Widget& rhs);//交换rhs和this  
  5.     Widget& operator=(const Widget& rhs)  
  6.     {  
  7.         Widget tmp(rhs);//赋值一份数据  
  8.         swap(tmp)//交换  
  9.         return *this;//临时变量会自动销毁  
  10.     }  
  11.     int *p;  
  12. };  
如果赋值操作符参数是值传递,那么就不需要新建临时变量,直接使用函数参数即可。
[cpp]  view plain  copy
 
  1. class Widget  
  2. {  
  3. public:  
  4.     void swap(const Widget& rhs);//交换rhs和this  
  5.     Widget& operator=(const Widget rhs)  
  6.     {  
  7.         swap(rhs)  
  8.         return *this;  
  9.     }  
  10.     int *p;  
  11. };  
这个做法代码可读性比较差,但是将“copying动作”从函数体内移到“函数参数构造阶段”,编译器有时会生成效率更高的代码(by moving the copying operation from the body of the function to construction of the parameter, it's fact that compiler can sometimes generate more efficient code.


条款12:复制对象时勿忘其每一部分

在一个类中,有两个函数可以给复制对象:复制构造函数和赋值操作符,统称为copying函数。在条款5中讲到,如果我们自己不编写者两个函数,编译器会帮我们实现这两个函数,编译器生成的版本会将对象的所有成员变量做一份拷贝。编译器生成的copying函数的做法通常是浅拷贝。可以参考 这里。
如果我们自己实现了copying函数,编译器就不再帮我们实现。但是编译器不会帮我们检查copying函数是否给对象的每一个变量都赋值。
下面有一个消费者的类
[cpp]  view plain  copy
 
  1. class Cutsomer  
  2. {  
  3. public:  
  4.     Cutsomer()  
  5.     {  
  6.         name="nobody";  
  7.     }  
  8.     Cutsomer(const Cutsomer& rhs)  
  9.         :name(rhs.name)  
  10.     {  
  11.         cout<<"Customer Copy Ctor"<<endl;  
  12.     }  
  13.     Cutsomer& operator=(const Cutsomer& rhs)  
  14.     {  
  15.         cout<<"assign operator"<<endl;  
  16.         name=rhs.name;  
  17.         return *this;  
  18.     }  
  19. private:  
  20.     string name;  
  21. };  
这样的copying函数没有问题,但是如果再给类添加变量,例如添加一个电话号码
[cpp]  view plain  copy
 
  1. class Cutsomer  
  2. {  
  3. ……  
  4. private:  
  5.     string name;  
  6.     string telphone;  
  7. };  
这时copying函数不做更改,即便是在最高警告级别,编译器也不会报错,但是我们的确少拷贝了内容。
由此可以得出结论:一旦给类添加变量,那么自己编写的copying函数也要修改,因为编译器不会提醒你。
在派生类层次中,这样的bug更难发现。假如有优先级的客户类,它继承自Customer
[cpp]  view plain  copy
 
  1. class PriorityCustomer:public Cutsomer  
  2. {  
  3. public:  
  4.     PriorityCustomer()  
  5.     {  
  6.         cout<<"PriorityCustomer Ctor"<<endl;  
  7.     }  
  8.     PriorityCustomer(const PriorityCustomer& rhs)  
  9.         :priority(rhs.priority)  
  10.     {  
  11.         cout<<"PriorityCustomer Copy Ctor"<<endl;  
  12.     }  
  13.     PriorityCustomer& operator=(const PriorityCustomer& rhs)  
  14.     {  
  15.         cout<<"PriorityCustomer assign operator"<<endl;  
  16.         priority=rhs.priority;  
  17.         return *this;  
  18.     }  
  19. private:  
  20.     int priority;  
  21. };  
在PriorityCustomer的copying函数中,只是复制了PriorityCustomer部分的内容,基类内容被忽略了。那么其基类内容部分怎么初始化的呢?
在派生类中构造函数没有初始化的基类部分是通过基类默认构造函数初始化的(没有默认构造函数就会报错)。
但是在copy assignment操作符中,不会调用基类的默认构造函数,因为copy assignment只是给对象重新赋值,不是初始化,因此不会调用基类的构造函数,除非我们显示调用。
正确的PriorityCustomer的copying函数应该这样写:
[cpp]  view plain  copy
 
  1. PriorityCustomer(const PriorityCustomer& rhs)  
  2.         :Cutsomer(rhs),priority(rhs.priority)  
  3.     {  
  4.         cout<<"PriorityCustomer Copy Ctor"<<endl;  
  5.     }  
  6.     PriorityCustomer& operator=(const PriorityCustomer& rhs)  
  7.     {  
  8.         cout<<"PriorityCustomer assign operator"<<endl;  
  9.         Cutsomer::operator=(rhs);  
  10.         priority=rhs.priority;  
  11.         return *this;  
  12.     }  

可以发现复制构造函数和赋值操作符有类似的代码。但是者两个函数不能相互调用。复制构造函数是构造一个不存在的对象,而赋值操作符是给一个存在的对象重新赋值。消除重复代码的方法编写一个private方法,例如void Init()。在这个函数中操作重复代码。

你可能感兴趣的:(构造/析构/赋值 函数)