前言
浅拷贝:简单的赋值操作,没有指针的指针成员申请新的内存空间,在析构指向堆内存空间的变量时,往往会因为多次析构导致程序错误。
深拷贝:在堆区重新申请空间,进行拷贝,不会出现因为浅拷贝类似的程序错误问题。
一、使用步骤
1.浅拷贝
代码如下(示例):
#include<bits/stdc++.h>
using namespace std;
class person
{
public:
person()
{
cout<<"调用person的无参构造函数"<<endl;
}
person(int a,int height)
{
age=a;//必要的赋值操作,没有的话在输出阶段会输出乱码;
m_height=new int(height);
cout<<"调用person的有参构造函数"<<endl;
}
int age;
int *m_height;
~person()
{
//将在堆区开辟数据的值做释放
//-----------------------------注释区域
if(m_height !=NULL)
{
delete m_height;
m_height = NULL;
} //--------------------------------------------注释区域
cout<<"调用person的析构函数"<<endl;
}
};
void test1()
{
person p1(18,180);
cout<<"p1打印年龄: "<<p1.age<<"打印身高: "<<*p1.m_height<<endl;
person p2(p1);
cout<<"p2打印年龄: "<<p2.age<<"打印身高: "<<*p2.m_height<<endl;
}
int main()
{
test1();
return 0;
}
注释前
注释后
相比之下,因为内存提前备放空,导致程序终止,析构函数无法调用两次
2.深拷贝
代码如下(示例):
#include<bits/stdc++.h>
using namespace std;
class person
{
public:
person()
{
cout<<"调用person的无参构造函数"<<endl;
}
person(int a,int height)
{
age=a;//必要的赋值操作,没有的话在输出阶段会输出乱码;
m_height=new int(height);
cout<<"调用person的有参构造函数"<<endl;
}
int age;
int *m_height;
person(const person &p)
{
cout<<"person拷贝函数调用"<<endl;
age =p.age;
//m_height=p.m_height;编译器默认实现的代码
//深拷贝
m_height=new int(*p.m_height);
}
~person()
{
//将在堆区开辟数据的值做释放
if(m_height !=NULL)
{
delete m_height;
m_height = NULL;
}
cout<<"调用person的析构函数"<<endl;
}
};
void test1()
{
person p1(18,180);
cout<<"p1打印年龄: "<<p1.age<<"打印身高: "<<*p1.m_height<<endl;
person p2(p1);
cout<<"p2打印年龄: "<<p2.age<<"打印身高: "<<*p2.m_height<<endl;
}
int main()
{
test1();
return 0;
}
总结
浅拷贝就像拷贝的和被拷贝的共有一份面包(数据),一旦被吃了(释放),别人就什么都没有了,深拷贝是拷贝的人自己买了同样的面包(构建空间写入数据),被拷贝的面包(数据)吃与不吃(释放或者被释放)都和拷贝的没有关系了。