1.说明什么是拷贝构造函数
对于普通的变量来说,拷贝是非常简单的,但是对于类来说有很多成员变量,该怎么拷贝呢?
int a = 10;
int b = a;//这里复制是很简单的
class Cpp{
int a;
int b;
....
};
Cpp a;
Cpp a =b;//这里如何复制呢
下面一个列子:
#include <cstdlib>
#include <iostream>
using namespace std;
class CppEx{
private:
int a;
public:
CppEx(int b){
a = b;
}
void show()
{
cout<<a<<endl;
}
};
int main(int argc, char *argv[])
{
CppEx t(100);
CppEx b = t;//这里调用的是拷贝构造函数
b.show();
return 0;
}
输入的结果是100,但是这个例子不能说明是拷贝构造函数起到的作用,看起来还是在复制。
下面例举出拷贝构造函数的例子,以说明上面的是拷贝构造函数起到的作用,而不是简单的复制。
#include <cstdlib>
#include <iostream>
using namespace std;
class CppEx{
private:
int a;
public:
CppEx(int b){
a = b;
cout<<"Create Class"<<endl;
}
CppEx(const CppEx &cppEx){
a = cppEx.a;
cout<<"Cpp Copy"<<endl;
}
void show()
{
cout<<a<<endl;
}
};
int main(int argc, char *argv[])
{
CppEx t(100);
CppEx b = t;//这里调用的是拷贝构造函数
b.show();
t.show();
return 0;
}
输入结果是:
Create Class
Cpp Copy
100
100
当我们创建t的时候,调用了构造函数,但是我们创建b的时候为什么没有调用构造函数呢,原因调用了拷贝构造函数,而不是简单的复制.
2.什么时候调用拷贝构造函数
①对象以值传递方式传入
#include <cstdlib>
#include <iostream>
using namespace std;
class CppEx{
private:
int a;
public:
CppEx(int b){
a = b;
cout<<"Create Class"<<endl;
}
CppEx(const CppEx &cppEx){
a = cppEx.a;
cout<<"Cpp Copy"<<endl;
}
void show()
{
cout<<a<<endl;
}
~CppEx(){
cout<<"free"<<endl;
}
};
void gFunc(CppEx k)
{
cout<<"gFunc"<<endl;
}
int main(int argc, char *argv[])
{
CppEx t(100);
gFunc(t);
return 0;
}
输出结果:
Create Class
Cpp Copy
gFunc
free
free
调用gFunc的步骤
1)当t的传入gFunc时,会生成一个临时变量
2)然后调用拷贝构造函数把临时变量的值传递给形参k
3)当gFunc函数返回的时候析构k
②对象值以值传递从函数返回
#include <cstdlib>
#include <iostream>
using namespace std;
class CppEx{
private:
int a;
public:
CppEx(int b){
a = b;
cout<<"Create Class"<<endl;
}
CppEx(const CppEx &cppEx){
a = cppEx.a;
cout<<"Cpp Copy"<<endl;
}
void show()
{
cout<<a<<endl;
}
~CppEx(){
cout<<"free"<<endl;
}
};
CppEx gFunc(CppEx k)
{
cout<<"gFunc"<<endl;
return k;
}
int main(int argc, char *argv[])
{
CppEx t(100);
gFunc(t);
return 0;
}
输入结果是:
Create Class
Cpp Copy
gFunc
Cpp Copy
free
free
free
两次复制构造:
第一次:调用函数产生临时变量,临时变量拷贝给k
第二次:返回值产生(这里我还是有点混乱,高手指点)
三次free:
第一次free:调用gFunc函数时候产生的临时变量被析构
第二次free:当返回k时,没有变量接收此返回值,所以直接析构
第三次free:t的析构
3.浅拷贝和深拷贝
①默认拷贝构造函数
简单的对对象成员一一赋值(不会处理静态数据成员)
#include<iostream>
using namespace std;
class Rect{
private:
int width;
int height;
static int count;
public:
Rect(){
count++;
}
~Rect(){
count--;
}
static int getCount()
{
return count;
}
};
int Rect::count = 0;
int main()
{
Rect rect1;
cout<<"The count of rect :"<<Rect::getCount()<<endl;
Rect rect2(rect1);
cout<<"The count of Rect:"<<Rect::getCount()<<endl;
return 0;
}
输出结果是:
The count of rect :1
The count of Rect:1
可以看出,拷贝构造函数没有处理静态数据成员。
出现这个问题的本质原因在于赋值对象的时候,计数器没有递增,解决办法如下:
#include<iostream>
using namespace std;
class Rect{
private:
int width;
int height;
static int count;
public:
Rect(){
count++;
}
Rect(Rect &r){
width = r.width;
height = r.height;
count++;
}
~Rect(){
count--;
}
static int getCount()
{
return count;
}
};
int Rect::count = 0;
int main()
{
Rect rect1;
cout<<"The count of rect :"<<Rect::getCount()<<endl;
Rect rect2(rect1);
cout<<"The count of Rect:"<<Rect::getCount()<<endl;
return 0;
}
输出结果如下:
The count of rect :1
The count of Rect:2
②浅拷贝
所谓浅拷贝,指的是在对象赋值时,只对对象中的数据进行简单的赋值,
默认拷贝构造构造函数也是浅拷贝。但是对象成员是动态成员,浅拷贝
就会出现问题。
#include<iostream>
using namespace std;
class Rect{
private:
int width;
int height;
int *p;
public:
Rect(){
p = new int(100);
}
~Rect(){
if(p !=NULL)
delete p;
}
};
int main()
{
Rect rect1;
Rect rect2(rect1);
return 0;
}
这个程序编译的时候是没有问题,问题出现在运行时。
原因是当rect1拷贝给rect2时,rect2的 p成员指针指的内
存和rect1对象的p成员指针指的内存是一样的。当执行完之后
析构函数要析构rect1和2 所以肯定是错误的。
解决的办法是深度拷贝。
三,深入拷贝
对成员不仅是简单拷贝,还要分配内存等操作。
#include<iostream>
using namespace std;
class Rect{
private:
int width;
int height;
int *p;
public:
Rect(){
p = new int(100);
}
Rect(Rect &r){
width = r.width;
height = r.height;
p = new int;
*p = *(r.p);
}
~Rect(){
if(p !=NULL)
delete p;
}
};
int main()
{
Rect rect1;
Rect rect2(rect1);
return 0;
}
防止默认拷贝发生的方法,在private里申明一个拷贝构造函数