c++中深淺拷貝以及寫時(shí)拷貝的實(shí)現(xiàn)示例代碼
本文主要給大家介紹了關(guān)于c++中深淺拷貝及寫時(shí)拷貝實(shí)現(xiàn)的相關(guān)內(nèi)容,分享出來供大家參考學(xué)習(xí),下面話不多說,來一起看看詳細(xì)的介紹:
一:淺拷貝&深拷貝
淺拷貝:在拷貝構(gòu)造的時(shí)候,直接將原內(nèi)容的地址交給要拷貝的類,兩個(gè)類共同指向一片空間。但是存在很大的缺陷:①一旦對(duì)s2進(jìn)行操作,s1的內(nèi)容也會(huì)改變;②析構(gòu)時(shí)先析構(gòu)s2,再析構(gòu)s1,但是由于s1,s2指向同一片空間,會(huì)導(dǎo)致一片空間的二次析構(gòu)導(dǎo)致出錯(cuò)。
深拷貝:通過開辟和源空間大小相同的空間并將內(nèi)容拷貝下來再進(jìn)行操作。不論是否對(duì)s2進(jìn)行操作,都會(huì)拷貝一片相同大小的空間以及內(nèi)容下來。
圖示如下:

深拷貝實(shí)現(xiàn)如下:
#include <iostream>
using namespace std;
class String
{
public:
String(char* str = "")
:_str(new char[strlen(str)+1])
{
strcpy(_str, str);
}
//傳統(tǒng)寫法,開辟空間
String(const String& s)
{
_str = new(char[strlen(s._str) + 1]);
strcpy(_str, s._str);
}
//現(xiàn)代寫法,利用構(gòu)造函數(shù)
//String(const String& s)
// :_str(NULL)
//{
// String tmp(s._str);
// swap(_str, tmp._str);
//}
//****************賦值運(yùn)算符重載**************
//String& operator=(const String& s)
//{
// if (this != &s)
// {
// delete[] _str;
// _str = new char[strlen(s._str) + 1];
// strcpy(_str, s._str);
// }
// return *this;
//}
//****************賦值運(yùn)算符重載**************
String& operator=(String& s)
{
swap(_str, s._str);
return *this;
}
//***************析構(gòu)函數(shù)********************
~String()
{
if (_str)
{
delete[] _str;
}
}
private:
char* _str;
};
二:寫時(shí)拷貝
寫時(shí)拷貝:引入一個(gè)計(jì)數(shù)器,每片不同內(nèi)容的空間上都再由一個(gè)計(jì)數(shù)器組成,在構(gòu)造第一個(gè)類指向時(shí),計(jì)數(shù)器初始化為1,之后每次有新的類也指向同一片空間時(shí),計(jì)數(shù)器加價(jià);在析構(gòu)時(shí)判斷該片空間對(duì)應(yīng)計(jì)數(shù)器是否為1,為1則執(zhí)行清理工作,大于1則計(jì)數(shù)器-1。如果有需要進(jìn)行增刪等操作時(shí),再拷貝空間完成,有利于提高效率。
寫法一:
#include <iostream>
using namespace std;
class String
{
public:
String(char* str = "")
:_str(new char[strlen(str)]+1)
, _refCount(new int(1))
{
strcpy(_str, str);
}
String(const String& str)
: _str(str._str)
,_refCount(str._refCount)
{
(*_refCount)++;
}
~String()
{
release();
}
String& operator= (const String& s)
{
if (_str != s._str)
{
release();
_refCount = s._refCount;
(*_refCount)++;
_str = s._str;
}
return *this;
}
void release()
{
if ((*--_refCount) == 0)
{
delete[] _str;
delete _refCount;
}
}
private:
char* _str;
int* _refCount;
};
缺點(diǎn):每構(gòu)造一個(gè)新類,就會(huì)多開四個(gè)字節(jié),會(huì)導(dǎo)致空間中有許多的內(nèi)存碎片。
第二種:
class String
{
public:
String(char* str = "")
:_str(new char[strlen(str)+1+4])
{
*(int*)_str = 1;
_str += 4;
strcpy(_str, str);
}
String(const String& s)
:_str(s._str)
{
++GetCount();
}
~String()
{
release();
}
String& operator=(const String& s)
{
if (this != &s)
{
realease();
_str = s._str;
GetCount()++;
}
return *this;
}
void release()
{
if (--GetCount() == 0)
{
_str -= 4;
delete[] _str;
}
}
int& GetCount()
{
return *((int*)_str - 1);
}
private:
char* _str;
};
注意:由于計(jì)數(shù)器存放在了_str首地址-4的地址上,所以在析構(gòu)時(shí)一定要注意全部釋放,避免內(nèi)存泄漏。
圖示如下:

總結(jié)
以上就是這篇文章的全部?jī)?nèi)容了,希望本文的內(nèi)容對(duì)大家的學(xué)習(xí)或者工作能帶來一定的幫助,如果有疑問大家可以留言交流,謝謝大家對(duì)腳本之家的支持
相關(guān)文章
C++實(shí)現(xiàn)LeetCode(96.獨(dú)一無二的二叉搜索樹)
這篇文章主要介紹了C++實(shí)現(xiàn)LeetCode(96.獨(dú)一無二的二叉搜索樹),本篇文章通過簡(jiǎn)要的案例,講解了該項(xiàng)技術(shù)的了解與使用,以下就是詳細(xì)內(nèi)容,需要的朋友可以參考下2021-07-07
教你如何使用qt quick-PathView實(shí)現(xiàn)好看的home界面
pathView的使用類似與ListView,都需要模型(model)和代理(delegate),只不過pathView多了一個(gè)路徑(path)屬性,顧名思義路徑就是item滑動(dòng)的路徑,下面給大家分享qt quick-PathView實(shí)現(xiàn)好看的home界面,一起看看吧2021-06-06
C++中賦值運(yùn)算符與逗號(hào)運(yùn)算符的用法詳解
這篇文章主要介紹了C++中賦值運(yùn)算符與逗號(hào)運(yùn)算符的用法詳解,是C++入門學(xué)習(xí)中的基礎(chǔ)知識(shí),需要的朋友可以參考下2015-09-09

