1. 为什么要使用拷贝构造函数?

    请看以下程序,我们创建了两个Node对象,第二个对象的创建直接拷贝第一个已创建的对象。然后我们想修改第二个对象node2的name字段,采用库函数strcpy()。但是并没有得到预期的结果。
   
拷贝构造函数与重载赋值操作符的使用[C++][code]#include<iostream.h>
拷贝构造函数与重载赋值操作符的使用[C++][code]#include
<string.h>
拷贝构造函数与重载赋值操作符的使用[C++][code]
拷贝构造函数与重载赋值操作符的使用[C++][code]
struct Node
}

运行结果:
Wendy 20 Wendy 30

    解决办法是使用显式的拷贝构造函数,程序如下所示:

拷贝构造函数与重载赋值操作符的使用[C++][code]#include<iostream.h>
拷贝构造函数与重载赋值操作符的使用[C++][code]#include
<string.h>

拷贝构造函数与重载赋值操作符的使用[C++][code]
struct Node
}

运行结果:
Roger 20 Wendy 30

2. 什么时候需要显式创建复制构造函数?

    假定对象包含一个用来动态为对象分配内存的指针,如果创建了对象的逐位复制件,它会包含指向与原对象中的指针所指向的相同的内存位置的指针。当对象的复制件销毁时,析构函数将会释放仍然由原始对象使用的内存。原始对象将会因此销毁,从而产生错误。为防止这种类型的错误,应该使用复制构造函数
    注意:析构函数的调用次数等于全部构造函数(常规构造与复制构造函数)的调用总数
    
    拷贝构造函数使用举例:

拷贝构造函数与重载赋值操作符的使用[C++][code]//program demonstrates a practical use of the copy constructor
拷贝构造函数与重载赋值操作符的使用[C++][code]
#include <iostream>
拷贝构造函数与重载赋值操作符的使用[C++][code]#include 
<cstring>
拷贝构造函数与重载赋值操作符的使用[C++][code]
using namespace std;
拷贝构造函数与重载赋值操作符的使用[C++][code]
拷贝构造函数与重载赋值操作符的使用[C++][code]
class Message
}

运行结果:
Normal constructor called! mes: 4726064
            Message: PROGRAM_1
Normal constructor called! mes: 4725424
Copy constructor called! mes: 4725344
            Message: copy constructor test!
Copy constructor called! mes: 4725696
Normal constructor called! mes: 4725616
            Message: COPY CONSTRUCTOR TEST!
Deconstructor called! mes: 4725616
Deconstructor called! mes: 4725696
Deconstructor called! mes: 4725344
Deconstructor called! mes: 4725424
Deconstructor called! mes: 4726064


3. 赋值运算符的重载
    
拷贝构造函数与重载赋值操作符的使用[C++][code]#include<iostream.h>
拷贝构造函数与重载赋值操作符的使用[C++][code]#include
<string.h>
拷贝构造函数与重载赋值操作符的使用[C++][code]
拷贝构造函数与重载赋值操作符的使用[C++][code]
struct Node
 

运行结果:
Roger 20 Wendy 30


3. 示例程序

}

运行结果:
-----------------------e.g. 1--------------------
2 boxes built so far.
                Adding boxes:
                2 boxes built so far.
                Total volume=30
2 boxes built so far.
-----------------------e.g. 2.2--------------------
 Enter x and y coordinates=>33 55  回车
        Copy constructor
                Pixel destroyed!
                Pixel destroyed!
Pixel's coordinates:
X=33 Y=55
        Copy constructor
        Copy constructor
                Pixel destroyed!
                Pixel destroyed!
Pixel's coordinates:
X=43 Y=65
Pixel's coordinates:
X=33 Y=55
-----------------------e.g. 2.1-------------------
        Normal constructor
 X=10 Y=20
        Copy constructor
 X=10 Y=20
        Copy constructor
        Copy constructor
                Pixel destroyed!
                Pixel destroyed!
 X=512 Y=512
-----------------------end of main()-----------------
                Pixel destroyed!
                Pixel destroyed!
                Pixel destroyed!
                Pixel destroyed!
                Box destroyed!
                Box destroyed!
Press any key to continue

相关文章:

  • 2022-12-23
  • 2021-08-27
  • 2022-12-23
  • 2022-12-23
  • 2021-09-25
  • 2021-12-07
  • 2021-06-21
猜你喜欢
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2021-04-10
相关资源
相似解决方案