“深拷贝”与“浅拷贝”是C++的基础,也是在程序员面试过程中经常被问到的问题之一。
浅拷贝:简单的赋值拷贝操作;
深拷贝:在堆区重新申请空间,进行拷贝操作;
默认拷贝构造函数——“浅拷贝”示例:
#include <iostream>
using namespace std;
// 定义Person类
class Person
{
public:
// 默认构造函数
Person()
{
cout << "Person类 默认构造函数调用" << endl;
}
// 有参构造函数
Person(int age,int height)
{
/*
该函数主要是对属性进行赋值操作,
相当于一个外部接口(在Person类外部调用该有参构造函数可对Perrson类的属性进行赋值)
*/
m_Age = age; // 将外部传进来的 int age 赋值给年龄属性 m_Age
m_Height = new int(height); // 将 int height 创建在堆区,返回的是地址,即 m_Height 是指针,用来接收创建在堆区的 height 的地址
cout << "Person类 有参构造函数调用" << endl;
}
// 析构函数
~Person()
{
// 析构代码的作用:将堆区开辟的数据做释放操作
if(m_Height != NULL)
{
delete m_Height; // 释放堆区数据
m_Height = NULL; // 避免野指针
}
cout << "Person类 析构函数调用" << endl;
}
int m_Age; // 年龄属性
int* m_Height; // 身高属性
};
void test01()
{
Person p1(18,160); // 有参构造函数的调用,实例化对象,即利用Person类创建了对象p1
cout << "p1 的年龄为:" << p1.m_Age << endl;
cout << "p1 的身高为:" << *p1.m_Height << endl;
Person p2(p1); // 默认拷贝构造函数的调用,浅拷贝
cout << "p2 的年龄为:" << p2.m_Age << endl;
cout << "p2 的身高为:" << *p2.m_Height << endl;
}
int main() {
test01();
system("pause");
return 0;
}
运行该段代码,系统会崩掉,如图:
原因:
在运行该段代码时,我们实例化了 p1 和 p2 两个对象,两个对象均有 m_Age 和 m_Height 两个属性,其中 p2 是通过默认拷贝构造函数创建的,实际上 p2 对象仅仅对 p1 对象的 m_Age 属性和 m_Height 属性进行了赋值拷贝操作——“浅拷贝”,我们知道 m_Age 是一个 int 类型,那么该段代码的问题就出现在 m_Height 属性上,因为 m_Height 是一个指针,假设我们在堆区创建的 height 数据的地址为 0x0011(代码段为 new int(height)),那么 p1 对象的 m_Height 属性就是 0x0011,它指向的是堆区的数据 height,在创建对象 p2 时,由于我们是做的“浅拷贝”,仅仅将 0x0011 这个地址进行了拷贝,那么当代码运行析构函数时,需要对堆区的数据进行释放,根据“先进后出”的原则, p2 对象的 m_Height 属性不为NULL,则该指针指向的堆区的数据 height 被释放,此时代码仍然没有问题,那么接下来,当析构函数对 p1 对象的 m_Height 指针指向的堆区的数据 height 进行释放时,由于 p2 已经释过了,此时再进行释放就会报错。所以,所以“浅拷贝”的问题主要是:堆区的内存被重复释放。“浅拷贝”的问题需要用“深拷贝”来解决。
自定义拷贝构造函数实现“深拷贝”
“深拷贝”需要自定义拷贝构造函数以解决“浅拷贝”带来的问题:
Person(const Person &p)
{
cout << "Person类 自定义拷贝构造函数调用" << endl;
m_Age = p.m_Age;
m_Height = new int(*p.m_Height); // “深拷贝”原理
// m_Height = p.m_Height; 这是“浅拷贝"的原理
}
m_Height = new int(*p.m_Height);是重点,该段代码的含义是在堆区重新开辟空间用于存储数据,这样避免了“浅拷贝”重复释放堆区内存的问题。
完整代码:
#include <iostream>
using namespace std;
// 定义Person类
class Person
{
public:
// 默认构造函数
Person()
{
cout << "Person类 默认构造函数调用" << endl;
}
// 有参构造函数
Person(int age,int height)
{
/*
该函数主要是对属性进行赋值操作,
相当于一个外部接口(在Person类外部调用该有参构造函数可对Perrson类的属性进行赋值)
*/
m_Age = age; // 将外部传进来的 int age 赋值给年龄属性 m_Age
m_Height = new int(height); // 将 int height 创建在堆区,返回的是地址,即 m_Height 是指针,用来接收创建在堆区的 height 的地址
cout << "Person类 有参构造函数调用" << endl;
}
// 拷贝构造函数
Person(const Person &p)
{
cout << "Person类 自定义拷贝构造函数调用" << endl;
m_Age = p.m_Age;
m_Height = new int(*p.m_Height); // “深拷贝”原理
// m_Height = p.m_Height; 这是“浅拷贝"的原理
}
// 析构函数
~Person()
{
// 析构代码的作用:将堆区开辟的数据做释放操作
if(m_Height != NULL)
{
delete m_Height; // 释放堆区数据
m_Height = NULL; // 避免野指针
}
cout << "Person类 析构函数调用" << endl;
}
int m_Age; // 年龄属性
int* m_Height; // 身高属性
};
void test01()
{
Person p1(18,160); // 有参构造函数的调用,实例化对象,即利用Person类创建了对象p1
cout << "p1 的年龄为:" << p1.m_Age << endl;
cout << "p1 的身高为:" << *p1.m_Height << endl;
Person p2(p1); // 默认拷贝构造函数的调用,浅拷贝
cout << "p2 的年龄为:" << p2.m_Age << endl;
cout << "p2 的身高为:" << *p2.m_Height << endl;
}
int main() {
test01();
system("pause");
return 0;
}
运行结果: