C++ 拷贝构造函数

发布时间 2024-01-10 17:23:30作者: ᶜʸᵃⁿ

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

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

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

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

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

 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 << "调用构造函数" << endl;
21     // 为指针分配内存
22     ptr = new int;
23     *ptr = len;
24 }
25  
26 Line::Line(const Line &obj)
27 {
28     cout << "111111111111111111111111111" << endl;
29     cout << "调用拷贝构造函数并为指针 ptr 分配内存" << endl;
30     cout << "222222222222222222222222222" << endl;
31     ptr = new int;
32     *ptr = *obj.ptr; // 拷贝值
33 }
34  
35 Line::~Line(void)
36 {
37     cout << "释放内存" << endl;
38     delete ptr;
39 }
40 int Line::getLength( void )
41 {
42     return *ptr;
43 }
44  
45 void display(Line obj)  //如果是display(Line &obj)则不会调用拷贝
46 {
47    cout << "line 大小 : " << obj.getLength() <<endl;
48 }
49  
50 // 程序的主函数
51 int main( )
52 {
53    Line line(10);
54     cout << "--------------------" << endl;
55  
56    display(line);
57  
58     cout << "999999999999999999999" << endl;
59    return 0;
60 }

结果是

调用构造函数
--------------------
111111111111111111111111111
调用拷贝构造函数并为指针 ptr 分配内存
222222222222222222222222222
line 大小 : 10
释放内存
999999999999999999999
释放内存

 

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

 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 << "调用构造函数" << endl;
21     // 为指针分配内存
22     ptr = new int;
23     *ptr = len;
24 }
25  
26 Line::Line(const Line &obj)
27 {
28     cout << "调用拷贝构造函数并为指针 ptr 分配内存" << endl;
29     ptr = new int;
30     *ptr = *obj.ptr; // 拷贝值
31 }
32  
33 Line::~Line(void)
34 {
35     cout << "释放内存" << endl;
36     delete ptr;
37 }
38 int Line::getLength( void )
39 {
40     return *ptr;
41 }
42  
43 void display(Line obj)
44 {
45    cout << "line 大小 : " << obj.getLength() <<endl;
46 }
47  
48 // 程序的主函数
49 int main( )
50 {
51    Line line1(10);
52    cout << "00000000000000000000000000" << endl;
53    Line line2 = line1; // 这里也调用了拷贝构造函数
54    cout << "11111111111111111111111111" << endl;
55    display(line1);
56    cout << "22222222222222222222222222" << endl;
57    display(line2);
58    cout << "33333333333333333333333333" << endl;
59    return 0;
60 }

结果是

调用构造函数
00000000000000000000000000
调用拷贝构造函数并为指针 ptr 分配内存
11111111111111111111111111
调用拷贝构造函数并为指针 ptr 分配内存
line 大小 : 10
释放内存
22222222222222222222222222
调用拷贝构造函数并为指针 ptr 分配内存
line 大小 : 10
释放内存
33333333333333333333333333
释放内存
释放内存   //最后这两个析构是line1和line2的