C++ operator= 注意事项

来源:互联网 发布:tomcat启动端口号 编辑:程序博客网 时间:2024/06/06 05:14

下面先看一段代码:

[cpp] view plain copy
 在CODE上查看代码片派生到我的代码片
  1. #include <iostream>  
  2. using namespace std;  
  3. class Point{  
  4. public:  
  5.     Point(float x=0.0,float y=0.0):_x(x),_y(y){}  
  6.     Point& operator=(const Point& rhs);   
  7.     void printData(){  
  8.         cout<<"_x="<<_x<<endl;  
  9.         cout<<"_y="<<_y<<endl;  
  10.     }  
  11. protected:  
  12.     float _x,_y;  
  13. };  
  14. inline Point& Point::operator=(const Point& p){  
  15.     cout<<"Point::operator="<<endl;  
  16.     _x=p._x;  
  17.     _y=p._y;  
  18.     return *this;  
  19. }  
  20.   
  21. class Point3d:public virtual Point{  
  22. public:  
  23.     Point3d(float x=0.0,float y=0.0,float z=0.0):Point(x,y),_z(z){}  
  24.     Point3d& operator=(const Point3d& p){  
  25.         if(this==&p)  
  26.             return *this;  
  27.         cout<<"Point3d::operator="<<endl;  
  28.         _z=p._z;  
  29.         return *this;  
  30.     }  
  31.     void printData(){  
  32.         Point::printData();  
  33.         cout<<"_z="<<_z<<endl;  
  34.     }  
  35. protected:  
  36.     float _z;  
  37. };  
  38. int main(){  
  39.     Point3d a(1,2,3),b(4,5,6);  
  40.     a=b;  
  41.     a.printData();  
  42.     return 0;  
  43. }  

程序输出结果如下:


分析:派生类定义了赋值操作符,但是并不会隐式的调用基类的赋值操作符,这和构造函数和拷贝构造函数不同。

在看看下面这段代码:

[cpp] view plain copy
 在CODE上查看代码片派生到我的代码片
  1. #include <iostream>  
  2. using namespace std;  
  3. class Point{  
  4. public:  
  5.     Point(float x=0.0,float y=0.0):_x(x),_y(y){}  
  6.     Point& operator=(const Point& rhs);   
  7.     void printData(){  
  8.         cout<<"_x="<<_x<<endl;  
  9.         cout<<"_y="<<_y<<endl;  
  10.     }  
  11. protected:  
  12.     float _x,_y;  
  13. };  
  14. inline Point& Point::operator=(const Point& p){  
  15.     cout<<"Point::operator="<<endl;  
  16.     _x=p._x;  
  17.     _y=p._y;  
  18.     return *this;  
  19. }  
  20.   
  21. class Point3d:public virtual Point{  
  22. public:  
  23.     Point3d(float x=0.0,float y=0.0,float z=0.0):Point(x,y),_z(z){}  
  24.     void printData(){  
  25.         Point::printData();  
  26.         cout<<"_z="<<_z<<endl;  
  27.     }  
  28. protected:  
  29.     float _z;  
  30. };  
  31. int main(){  
  32.     Point3d a(1,2,3),b(4,5,6);  
  33.     a=b;  
  34.     a.printData();  
  35.     return 0;  
  36. }  

程序输出结果如下:


分析:派生类没有定义自己的赋值操作符,编译器为派生类合成一个赋值操作符函数(合成的赋值操作符函数,会插入调用基类赋值操作符函数的代码),合成的赋值操作符函数会隐式的调用基类的赋值操作符函数。


再看最后一段代码:

[cpp] view plain copy
 在CODE上查看代码片派生到我的代码片
  1. #include <iostream>  
  2. using namespace std;  
  3. class Point{  
  4. public:  
  5.     Point(float x=0.0,float y=0.0):_x(x),_y(y){}  
  6.     Point& operator=(const Point& rhs);   
  7.     void printData(){  
  8.         cout<<"_x="<<_x<<endl;  
  9.         cout<<"_y="<<_y<<endl;  
  10.     }  
  11. protected:  
  12.     float _x,_y;  
  13. };  
  14. inline Point& Point::operator=(const Point& p){  
  15.     cout<<"Point::operator="<<endl;  
  16.     _x=p._x;  
  17.     _y=p._y;  
  18.     return *this;  
  19. }  
  20.   
  21. class Point3d:public virtual Point{  
  22. public:  
  23.     Point3d(float x=0.0,float y=0.0,float z=0.0):Point(x,y),_z(z){}  
  24.     Point3d& operator=(const Point3d& p){  
  25.         if(this==&p)  
  26.             return *this;  
  27.         Point::operator=(p);  
  28.         cout<<"Point3d::operator="<<endl;  
  29.         _z=p._z;  
  30.         return *this;  
  31.     }  
  32.     void printData(){  
  33.         Point::printData();  
  34.         cout<<"_z="<<_z<<endl;  
  35.     }  
  36. protected:  
  37.     float _z;  
  38. };  
  39. int main(){  
  40.     Point3d a(1,2,3),b(4,5,6);  
  41.     a=b;  
  42.     a.printData();  
  43.     return 0;  
  44. }  

程序输出结果如下:


分析:派生类定义的赋值操作符函数显示调用基类定义的赋值操作符函数,先对基类对象部分赋值,再对派生类对象赋值。

0 0
原创粉丝点击