C++复制控制

来源:互联网 发布:如何评价列宁 知乎 编辑:程序博客网 时间:2024/05/17 05:01
 当定义一个新类型时,通过使用复制构造函数,赋值操作符,析构函数来显示或隐式地完成对象的复制,赋值,撤销 

1.复制构造函数:只有单个形参,而且该形参是对本类类型对象的引用(通常是const修饰)的构造函数,称为~ 
复制构造函数可用于: 
1)根据另一个同类型的对象显示或隐式初始化一个对象. 
2)复制一个对象,将它作为实参传给一个函数 
3)从函数返回时复制一个对象 
4)初始化顺序容器中的元素 
5)根据元素初始化式列表初始化数组元素
 
a.对象初始化形式 
  直接初始化:一般是将初始化式放在圆括号内 
  复制初始化:使用=符号 
  string str = "123"; //copy-initialization 
  string str1 = str; //copy-initialization 
b.形参与返回值 
  当形参或返回值为类类型时,由复制构造函数进行复制 
  string make_copy(size_t, const string&, const string&); 
c.初始化容器元素 
  vector<string> sevc(5); 
  先用string默认构造函数创建一个临时值为初始化svec,然后使用复制构造函数将临时值复制到sevc的每个元素 

2.合成的复制构造函数 

如果我们没有定义复制构造函数,编译器会为我们合成一个复制构造函数。

与合成的默认构造函数不同,即使我们定义了其他构造函数,也会合成复制构造函数。

合成复制构造函数的行为:执行逐个成员初始化,将新对象初始化原对象的副本 


3.定义自己的复制构造函数 
MyClass(const MyClass &mc) {//...} 
复制构造函数一般不设置为explicit 
4.禁止复制 
可以将复制构造函数显示的声明为private 
5.赋值操作符:对于已初始化过类类型的对象而言,对其赋值时调用赋值操作符函数 
MyClass& operator=(const MyClass&) {//...   return *this} 
6.合成的赋值操作符 
逐个成员赋值 
一般赋值操作符与复制构造函数一起使用 

7.析构函数:撤销类对象,自动调用 
三法则:如果类需要析构函数,则其也需要复制构造函数和赋值操作符 
~MyClass() {//...} 
8.合成析构函数:按照声明的逆序对非static对象进行撤销,但不能删除指针成员所指向的对象 

示例: 
头文件,rectangle.h 
C++代码  收藏代码
  1. /* 
  2. language:c++ 
  3. author:longsy 
  4. file:rectangle.h 
  5. */  
  6. namespace longsy {  
  7.     class Rectangle {  
  8.         int width;  
  9.         int height;  
  10.     public:  
  11.                  //带默认实参的默认构造函数  
  12.         Rectangle(int w=0, int h=0);  
  13.             //复制构造函数  
  14.                   Rectangle(const Rectangle & rect);  
  15.                   //赋值操作符  
  16.         Rectangle& operator=(const Rectangle &rect);  
  17.         //析构造函数  
  18.                   ~Rectangle();  
  19.               
  20.         void Draw();  
  21.     };  
  22. }  

实现文件,rectangle.cpp 
C++代码  收藏代码
  1. /* 
  2. language:c++ 
  3. author:longsy 
  4. file:rectangle.cpp 
  5. */  
  6. #include "rectangle.h"  
  7. using namespace longsy;  
  8.   
  9. #include <iostream>  
  10.   
  11. Rectangle::Rectangle(int w,int h) : width(w),height(h)   
  12. {  
  13.     std::cout << "Rectangle(int w,int h)" << std::endl;  
  14. }  
  15. Rectangle::Rectangle(const Rectangle &rect)   
  16. {  
  17.     this->width = rect.width;  
  18.     this->height = rect.height;  
  19.     std::cout << "Rectangle(const Rectangle &rect)" <<std::endl;  
  20. }  
  21. Rectangle& Rectangle::operator=(const Rectangle &rect)  
  22. {  
  23.     this->width = rect.width;  
  24.     this->height = rect.height;  
  25.     std::cout << "operator=(const Rectangle &rect)" << std::endl;  
  26.     return *this;  
  27. }  
  28. Rectangle::~Rectangle()  
  29. {  
  30.     this->width = 0;  
  31.     this->height = 0;  
  32.     std::cout << "~Rectangle()" << std::endl;     
  33. }  
  34.   
  35. void Rectangle::Draw()  
  36. {  
  37.     std::cout << "Draw the rectangle,width:" << this->width \  
  38. << ",height:" << height <<std::endl;  
  39. }  

测试文件,test.cpp 
C++代码  收藏代码
  1. /* 
  2. language:c++ 
  3. author:longsy 
  4. file:test.cpp 
  5. */  
  6. #include "rectangle.h"  
  7. using namespace longsy;  
  8.   
  9. int main()  
  10. {  
  11.     Rectangle rect; //调用默认构造函数  
  12.     rect.Draw();  
  13.       
  14.     Rectangle rect1(1,1); //Rectangle(int w,int h)  
  15.     rect1.Draw();  
  16.       
  17.     Rectangle rect2 = rect1;//调用复制构造函数  
  18.     rect2.Draw();  
  19.       
  20.     Rectangle rect3(2,2); //Rectangle(int w,int h)  
  21.     rect3.Draw();  
  22.     rect3 = rect1; //调用赋值操作符  
  23.     rect3.Draw();  
  24.       
  25.     return (0); //调用析构函数  
  26. }  
  27. /* 
  28. 运行结果: 
  29. Rectangle(int w,int h) 
  30. Draw the rectangle,width:0,height:0 
  31. Rectangle(int w,int h) 
  32. Draw the rectangle,width:1,height:1 
  33. Rectangle(const Rectangle &rect) 
  34. Draw the rectangle,width:1,height:1 
  35. Rectangle(int w,int h) 
  36. Draw the rectangle,width:2,height:2 
  37. operator=(const Rectangle &rect) 
  38. Draw the rectangle,width:1,height:1 
  39. ~Rectangle() 
  40. ~Rectangle() 
  41. ~Rectangle() 
  42. ~Rectangle() 
  43. */  


原创粉丝点击