淺析C++淺拷貝與深拷貝的聯(lián)系和區(qū)別
文章簡(jiǎn)述
c++中構(gòu)造函數(shù)分為三類:無參構(gòu)造、帶參構(gòu)造和拷貝構(gòu)造,其中拷貝構(gòu)造可分為默認(rèn)拷貝(淺拷貝)、深拷貝,在程序中,這里我們主要講淺拷貝和深拷貝的聯(lián)系和區(qū)別。
首先,我們要明白拷貝至少需要兩個(gè)對(duì)象,并且在拷貝時(shí),我們可以用const來保護(hù)原對(duì)象的內(nèi)容,具體用法:----- <類名>(const<類名> 對(duì)象) -----進(jìn)行復(fù)制的對(duì)象 ,當(dāng)我們使用棧開辟空間進(jìn)行復(fù)制時(shí),不會(huì)出現(xiàn)意外,如下:
/*=============================================== * 文件名稱:shallow_deep_copy.cpp * 創(chuàng) 建 者: * 創(chuàng)建日期: * 描 述: ================================================*/ #include <iostream> using namespace std; class student { private: string name; //--------------// int age; public: student() //出現(xiàn)帶參構(gòu)造,需要默認(rèn)構(gòu)造 { cout << "默認(rèn)構(gòu)造" << endl; } student(string m_name, int m_age) { cout << "name:" << m_name << endl << "age:" << m_age << endl; cout << "帶參構(gòu)造" << endl; } ~student() { cout << "析構(gòu)函數(shù)" << endl; } }; void test1() { student s1 = student("tom", 12); student s2 = s1; //利用棧進(jìn)行默認(rèn)復(fù)制 } int main() { test1(); //引用函數(shù). return 0; }
結(jié)果如圖:
name:tom
age:12
帶參構(gòu)造
析構(gòu)函數(shù)
析構(gòu)函數(shù)
我們可以發(fā)現(xiàn),復(fù)制后進(jìn)行輸出時(shí)只有一個(gè)帶參構(gòu)造和兩個(gè)析構(gòu)函數(shù),說明了棧中系統(tǒng)自動(dòng)申請(qǐng)又釋放了兩次空間,但是這個(gè)操作卻在自己創(chuàng)建空間(堆)時(shí),卻是錯(cuò)誤的,此時(shí)的代碼又有所不同:
/*=============================================== * 文件名稱:shallow_deep_copy.cpp * 創(chuàng) 建 者: * 創(chuàng)建日期: * 描 述: ================================================*/ #include <iostream> using namespace std; class student { private: string name; //int age; int *age; //定義指針age public: student() { cout << "默認(rèn)構(gòu)造" << endl; } #if 0 //------------------------------------------ student(string m_name, int m_age) { cout << "name:" << m_name << endl << "age:" << m_age << endl; cout << "帶參構(gòu)造" << endl; } #endif //------------------------------------------- student(int m_age) { this->age = new int(m_age); cout << "開辟空間" << endl; } ~student() { delete age; //開辟使用完成,在析構(gòu)進(jìn)行釋放age操作 cout << "析構(gòu)函數(shù)" << endl; } int get_age() { return *age; } }; void test1() { //student s1 = student("tom", 12); //student s2 = s1; student s1(10); //定義年齡為10,進(jìn)行復(fù)制 student s2 = s1; } int main() { test1(); //調(diào)用函數(shù) return 0; }
結(jié)果:
開辟空間
析構(gòu)函數(shù)
free(): double free detected in tcache 2
已放棄 (核心已轉(zhuǎn)儲(chǔ))
這里我們只調(diào)用了*age這個(gè)參數(shù),但已經(jīng)很明顯的看到結(jié)果,錯(cuò)誤信息為兩次釋放該空間,在開辟這片空間時(shí),又進(jìn)行重復(fù)釋放操作,很明顯這是系統(tǒng)所不允許的。
接下來進(jìn)行深拷貝:
/*=============================================== * 文件名稱:shallow_deep_copy.cpp * 創(chuàng) 建 者: * 創(chuàng)建日期: * 描 述: ================================================*/ #include <iostream> using namespace std; class student { private: string name; //int age; int *age; public: student() { cout << "默認(rèn)構(gòu)造" << endl; } #if 0 //-------------------------------------- student(string m_name, int m_age) { cout << "name:" << m_name << endl << "age:" << m_age << endl; cout << "帶參構(gòu)造" << 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); //創(chuàng)建一片新的空間進(jìn)行儲(chǔ)存 cout << "深拷貝" << endl; } //-------------------------------------- ~student() { delete age; cout << "析構(gòu)函數(shù)" << 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; }
開辟空間
深拷貝
析構(gòu)函數(shù)
析構(gòu)函數(shù)
此時(shí)結(jié)果為正確,這里釋放的是兩片一樣內(nèi)容但地址不一樣的空間。
到此,*我們就能總結(jié)出:淺拷貝(默認(rèn)拷貝)是兩個(gè)指針申請(qǐng)同一片空間地址,而在釋放時(shí),也是同一片堆內(nèi)存;深拷貝是對(duì)指針?biāo)赶虻膬?nèi)容進(jìn)行拷貝,同時(shí)兩個(gè)指針指向的是不同空間地址,所以在釋放時(shí)需要釋放兩次。
到此這篇關(guān)于淺析C++淺拷貝與深拷貝的聯(lián)系和區(qū)別的文章就介紹到這了,更多相關(guān)C++淺拷貝與深拷貝內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
C語(yǔ)言實(shí)現(xiàn)自動(dòng)存取款機(jī)模擬系統(tǒng)
這篇文章主要為大家詳細(xì)介紹了C語(yǔ)言實(shí)現(xiàn)自動(dòng)存取款機(jī)模擬系統(tǒng),文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-05-05利用反射獲得類的public static/const成員的值實(shí)例
下面小編就為大家?guī)硪黄梅瓷浍@得類的public static/const成員的值實(shí)例。小編覺得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2016-12-12QT應(yīng)用啟動(dòng)失敗排查方法小結(jié)
啟動(dòng)QT應(yīng)用經(jīng)常會(huì)碰到應(yīng)用啟動(dòng)失敗,qt platform plugin無法啟動(dòng),本文就來介紹一下QT應(yīng)用啟動(dòng)失敗排查方法小結(jié),具有一定的參考價(jià)值,感興趣的可以了解以下2023-09-09