C++ 拷贝构造函数


拷贝构造函数是一种特殊的构造函数,它在创建对象时,是使用同一类中之前创建的对象来初始化新创建的对象。拷贝构造函数通常用于:

  • 通过使用另一个同类型的对象来初始化新创建的对象。

  • 复制对象把它作为参数传递给函数。

  • 复制对象,并从函数返回这个对象。

如果在类中没有定义拷贝构造函数,编译器会自行定义一个。如果类带有指针变量,并有动态内存分配,则它必须有一个拷贝构造函数。拷贝构造函数的最常见形式如下:

  1. classname (const classname &obj) {
  2. // 构造函数的主体
  3. }

在这里,obj 是一个对象引用,该对象是用于初始化另一个对象的。

  1. #include <iostream>
  2.  
  3. using namespace std;
  4.  
  5. class Line
  6. {
  7. public:
  8. int getLength( void );
  9. Line( int len ); // 简单的构造函数
  10. Line( const Line &obj); // 拷贝构造函数
  11. ~Line(); // 析构函数
  12.  
  13. private:
  14. int *ptr;
  15. };
  16.  
  17. // 成员函数定义,包括构造函数
  18. Line::Line(int len)
  19. {
  20. cout << "Normal constructor allocating ptr" << endl;
  21. // 为指针分配内存
  22. ptr = new int;
  23. *ptr = len;
  24. }
  25.  
  26. Line::Line(const Line &obj)
  27. {
  28. cout << "Copy constructor allocating ptr." << endl;
  29. ptr = new int;
  30. *ptr = *obj.ptr; // copy the value
  31. }
  32.  
  33. Line::~Line(void)
  34. {
  35. cout << "Freeing memory!" << endl;
  36. delete ptr;
  37. }
  38. int Line::getLength( void )
  39. {
  40. return *ptr;
  41. }
  42.  
  43. void display(Line obj)
  44. {
  45. cout << "Length of line : " << obj.getLength() <<endl;
  46. }
  47.  
  48. // 程序的主函数
  49. int main( )
  50. {
  51. Line line(10);
  52.  
  53. display(line);
  54.  
  55. return 0;
  56. }

当上面的代码被编译和执行时,它会产生下列结果:

  1. Normal constructor allocating ptr
  2. Copy constructor allocating ptr.
  3. Length of line : 10
  4. Freeing memory!
  5. Freeing memory!

下面的实例对上面的实例稍作修改,通过使用已有的同类型的对象来初始化新创建的对象:

  1. #include <iostream>
  2.  
  3. using namespace std;
  4.  
  5. class Line
  6. {
  7. public:
  8. int getLength( void );
  9. Line( int len ); // 简单的构造函数
  10. Line( const Line &obj); // 拷贝构造函数
  11. ~Line(); // 析构函数
  12.  
  13. private:
  14. int *ptr;
  15. };
  16.  
  17. // 成员函数定义,包括构造函数
  18. Line::Line(int len)
  19. {
  20. cout << "Normal constructor allocating ptr" << endl;
  21. // 为指针分配内存
  22. ptr = new int;
  23. *ptr = len;
  24. }
  25.  
  26. Line::Line(const Line &obj)
  27. {
  28. cout << "Copy constructor allocating ptr." << endl;
  29. ptr = new int;
  30. *ptr = *obj.ptr; // copy the value
  31. }
  32.  
  33. Line::~Line(void)
  34. {
  35. cout << "Freeing memory!" << endl;
  36. delete ptr;
  37. }
  38. int Line::getLength( void )
  39. {
  40. return *ptr;
  41. }
  42.  
  43. void display(Line obj)
  44. {
  45. cout << "Length of line : " << obj.getLength() <<endl;
  46. }
  47.  
  48. // 程序的主函数
  49. int main( )
  50. {
  51. Line line1(10);
  52.  
  53. Line line2 = line1; // 这里也调用了拷贝构造函数
  54.  
  55. display(line1);
  56. display(line2);
  57.  
  58. return 0;
  59. }

当上面的代码被编译和执行时,它会产生下列结果:

  1. Normal constructor allocating ptr
  2. Copy constructor allocating ptr.
  3. Copy constructor allocating ptr.
  4. Length of line : 10
  5. Freeing memory!
  6. Copy constructor allocating ptr.
  7. Length of line : 10
  8. Freeing memory!
  9. Freeing memory!
  10. Freeing memory!