浅析C++浅拷贝与深拷贝的联系和区别

Valarie ·
更新时间:2024-09-20
· 1645 次阅读

文章简述

c++中构造函数分为三类:无参构造、带参构造和拷贝构造,其中拷贝构造可分为默认拷贝(浅拷贝)、深拷贝,在程序中,这里我们主要讲浅拷贝和深拷贝的联系和区别。

首先,我们要明白拷贝至少需要两个对象,并且在拷贝时,我们可以用const来保护原对象的内容,具体用法:----- <类名>(const<类名> 对象) -----进行复制的对象 ,当我们使用栈开辟空间进行复制时,不会出现意外,如下:

/*=============================================== * 文件名称:shallow_deep_copy.cpp * 创 建 者: * 创建日期: * 描 述: ================================================*/ #include <iostream> using namespace std; class student { private: string name; //--------------// int age; public: student() //出现带参构造,需要默认构造 { cout << "默认构造" << endl; } student(string m_name, int m_age) { cout << "name:" << m_name << endl << "age:" << m_age << endl; cout << "带参构造" << endl; } ~student() { cout << "析构函数" << endl; } }; void test1() { student s1 = student("tom", 12); student s2 = s1; //利用栈进行默认复制 } int main() { test1(); //引用函数. return 0; }

结果如图:

name:tom

age:12

带参构造

析构函数

析构函数

我们可以发现,复制后进行输出时只有一个带参构造和两个析构函数,说明了栈中系统自动申请又释放了两次空间,但是这个操作却在自己创建空间(堆)时,却是错误的,此时的代码又有所不同:

/*=============================================== * 文件名称:shallow_deep_copy.cpp * 创 建 者: * 创建日期: * 描 述: ================================================*/ #include <iostream> using namespace std; class student { private: string name; //int age; int *age; //定义指针age public: student() { cout << "默认构造" << endl; } #if 0 //------------------------------------------ student(string m_name, int m_age) { cout << "name:" << m_name << endl << "age:" << m_age << endl; cout << "带参构造" << endl; } #endif //------------------------------------------- student(int m_age) { this->age = new int(m_age); cout << "开辟空间" << endl; } ~student() { delete age; //开辟使用完成,在析构进行释放age操作 cout << "析构函数" << endl; } int get_age() { return *age; } }; void test1() { //student s1 = student("tom", 12); //student s2 = s1; student s1(10); //定义年龄为10,进行复制 student s2 = s1; } int main() { test1(); //调用函数 return 0; }

结果:

开辟空间

析构函数

free(): double free detected in tcache 2

已放弃 (核心已转储)

这里我们只调用了*age这个参数,但已经很明显的看到结果,错误信息为两次释放该空间,在开辟这片空间时,又进行重复释放操作,很明显这是系统所不允许的。

接下来进行深拷贝:

/*=============================================== * 文件名称:shallow_deep_copy.cpp * 创 建 者: * 创建日期: * 描 述: ================================================*/ #include <iostream> using namespace std; class student { private: string name; //int age; int *age; public: student() { cout << "默认构造" << endl; } #if 0 //-------------------------------------- student(string m_name, int m_age) { cout << "name:" << m_name << endl << "age:" << m_age << endl; cout << "带参构造" << endl; } #endif //-------------------------------------- student(int m_age) { this->age = new int(m_age); cout << "开辟空间" << endl; } //-----------深浅拷贝------------------- //student(const student &s) //{ // this->age = s.age; // cout << "浅拷贝" << endl; // //} student(const student &s) { this->age = new int(*s.age); //创建一片新的空间进行储存 cout << "深拷贝" << endl; } //-------------------------------------- ~student() { delete age; cout << "析构函数" << endl; } int get_age() { return *age; } }; void test1() { //student s1 = student("tom", 12); //student s2 = s1; student s1(10); student s2 = s1; } int main() { test1(); return 0; }

开辟空间

深拷贝

析构函数

析构函数

此时结果为正确,这里释放的是两片一样内容但地址不一样的空间。

到此,*我们就能总结出:浅拷贝(默认拷贝)是两个指针申请同一片空间地址,而在释放时,也是同一片堆内存;深拷贝是对指针所指向的内容进行拷贝,同时两个指针指向的是不同空间地址,所以在释放时需要释放两次。

到此这篇关于浅析C++浅拷贝与深拷贝的联系和区别的文章就介绍到这了,更多相关C++浅拷贝与深拷贝内容请搜索软件开发网以前的文章或继续浏览下面的相关文章希望大家以后多多支持软件开发网!



c+ 联系 深拷贝 浅拷贝 C++ 拷贝

需要 登录 后方可回复, 如果你还没有账号请 注册新账号