c++ primer 5th第13章拷贝控制知识点和自编习题答案
????????首先,先給大家提個醒。在網(wǎng)上的隨書源代碼里關(guān)于hasptr類的類指針版本編寫的移動構(gòu)造函數(shù)、移動賦值運算符、和析構(gòu)函數(shù)部分是有錯誤的。大家可以把hasptr累指針版本(里面帶移動構(gòu)造函數(shù)和移動賦值運算符部分)可以運行下,g++編譯環(huán)境發(fā)現(xiàn)core dumped錯誤。原因是:在代碼中途有的對象的use和ps所指的內(nèi)存已經(jīng)被釋放了,所以在析構(gòu)函數(shù)中再次直接釋放use和ps就是錯的了。正確應(yīng)該在中途每個地方use和ps被接管后都置為nullptr,然后在析構(gòu)函數(shù)中必須首先判斷use和ps是否是nullptr,然后再釋放。
************************************************************************************************************************************************
對nullptr指針,執(zhí)行delete操作會引發(fā)錯誤,g++提示:core dumped
************************************************************************************************************************************************
習(xí)題13.1拷貝、賦值和銷毀
1.拷貝初始化何時發(fā)生:
2.拷貝初始化的限制-----不能隱式使用explicit的構(gòu)造函數(shù)初始化一個類類型。?
?3.編譯器可以繞過拷貝構(gòu)造函數(shù)。
?習(xí)題13.1.1
練習(xí)13
答:拷貝構(gòu)造函數(shù)的名字和類名本身相同,參數(shù)類型是自身類型的引用,沒有返回值。像這樣的構(gòu)造函數(shù)叫做拷貝構(gòu)造函數(shù)。在用同類型的一個對象初始化另一個對象時候使用。
答:?在函數(shù)調(diào)用中,如果函數(shù)參數(shù)是非引用類型,要拷貝初始化,所以構(gòu)造函數(shù)的參數(shù)必須是引用。如果構(gòu)造函數(shù)自己的參數(shù)類型也是非引用,那么就會在拷貝參數(shù)的時候調(diào)用本身,如此反復(fù),則進(jìn)入無限循環(huán)。
練習(xí)13.2
答:StrBlob只有一個數(shù)據(jù)成員,類型是shared_ptr<vector<string>>,如果拷貝一個StrBlob,那么會拷貝shard_ptr對象,原來的shared_ptr的引用計數(shù)會增加1。新對象和原來對象會共享內(nèi)存。
?答:
第1處:local = arg。因為用同類型的另一個對象初始化本對象。
第二處:*heap = local;同理
第三處:Point pa[4] = {local,*heap};
當(dāng)然,這是這段代碼所發(fā)生的拷貝初始化。
?答:
class HasPtr{public:HasPtr(const string & s = string()):ps(new string(s)),i(0){}HasPtr(const HasPtr&);private:string *ps;int i; }; HasPtr::HasPtr(const HasPtr & p):ps(new string(*p.ps)),i(p.i){}13.1.2練習(xí)
練習(xí)13.6
答:
拷貝賦值運算符是定義類類型如何用另一個同類型對象給本對象賦值的運算符。有以下幾個特點:
返回類型是自身類型的引用、函數(shù)名字是operator=,參數(shù)類型是自身引用。
合成的賦值運算符完成用本類類型對象給自身類型對象的賦值任務(wù)。
如果編譯器沒有定義拷貝賦值運算符,那么編譯器會合成一個,叫做拷貝賦值運算符。
練習(xí)13.7
答:
當(dāng)用一個StrBlob對象給另一個StrBlob對象賦值的時候,shared_ptr對象對調(diào)用自己的拷貝賦值運算符,被賦值shared_ptr對象自身的引用計數(shù)會增加1,左側(cè)StrBlob對象的shared_ptr對象的引用計數(shù)減1。
????????????類StrBlobPtr對象有一個shared_ptr對象,一個week_ptr<vector<string>>對象和一個size_t對象。賦值的時候,右側(cè)shared_ptr對象的引用計數(shù)加1,左側(cè)的shared_ptr的引用計數(shù)減1,左側(cè)對象的shared_ptr對象指向右側(cè)的shared_ptr指向的內(nèi)存。對于weak_ptr對象,賦值后左側(cè)的weak_ptr的內(nèi)容換成右側(cè)類的weak_ptr的內(nèi)容。其它不變。curr的值也是賦值。
13.8
答:
class HasPtr{public:HasPtr(const string & s = string()):ps(new string(s)),i(0){}HasPtr(const HasPtr&);HasPtr& operator=(const HasPtr &);~HasPtr(){delete ps;}private:string *ps;int i; }; HasPtr::HasPtr(const HasPtr & p):ps(new string(*p.ps)),i(p.i){} HasPtr& HasPtr::operator=(const HasPtr &p){ //這里一定要爭取處理自賦值情況,也就是不能再拷貝之前釋放掉參數(shù)的成員ps指向的內(nèi)存 string *new_ps = new string(*p.ps); delete ps; ps = new_ps; i = p.i; return *this; }13.1.3析構(gòu)函數(shù)
析構(gòu)函數(shù)執(zhí)行與構(gòu)造函數(shù)相反的工作:構(gòu)造函數(shù)初始化對象的非static數(shù)據(jù)成員,還可能做一些其它工作;析構(gòu)函數(shù)釋放對象使用的資源,并銷毀對象的非static數(shù)據(jù)成員。
析構(gòu)函數(shù)是類的一個成員函數(shù),名字由波浪號接類名構(gòu)成。它沒有返回值,也不接受參數(shù)。析構(gòu)函數(shù)不接受參數(shù),所以不可以重載,一個類只有一個唯一的析構(gòu)函數(shù)。
析構(gòu)函數(shù)完成什么工作
首先執(zhí)行函數(shù)體,然后銷毀成員,成員按照初始化的順序的逆序銷毀。
在一個析構(gòu)函數(shù)中,不存在類似構(gòu)造函數(shù)中初始化列表的東西來控制成員如何銷毀,析構(gòu)部分是隱式的。成員銷毀時發(fā)生什么完全依賴于成員的類型。銷毀類類型成員需要執(zhí)行成員自己的析構(gòu)函數(shù)。內(nèi)置類型沒有析構(gòu)函數(shù),因此銷毀內(nèi)置類型成員什么也不需要做。
隱式銷毀一個內(nèi)置指針類型的成員不會delete它所指向的對象。
什么時候調(diào)用析構(gòu)函數(shù)
無論何時一個對象被銷毀,就會自動調(diào)用其析構(gòu)函數(shù):
如下面的例子:?
?p是一個內(nèi)置指針,塊末尾調(diào)用delete,p指向的內(nèi)存釋放。
局部對象p2的引用計數(shù)減1,如果引用計數(shù)變?yōu)?,那么釋放對象。
item,vec,塊結(jié)束后自動銷毀,釋放資源。
習(xí)題13.1.3節(jié)練習(xí)
練習(xí)13.9
答:
13.9
析構(gòu)函數(shù)的名字是波浪號加類名,參數(shù)列表為空的一個函數(shù)。和析構(gòu)函數(shù)的功能相反,構(gòu)造函數(shù)初始化對象,析構(gòu)函數(shù)負(fù)責(zé)銷毀對象,釋放對象占用的資源。
13.10
當(dāng)一個StrBlob銷毀時候,shared_ptr類會自動地釋放對象所占用的資源,銷毀對象。
StrBlobPtr對象銷毀的時候,weak_ptr對象會銷毀自己的對象。因為是一種弱引用,所以對原來的智能指針沒有任何影響。其它內(nèi)置類型的成員,則自動銷毀。
13.11
class HasPtr{public:HasPtr(const string & s = string()):ps(new string(s)),i(0){}HasPtr(const HasPtr&);~HasPtr(){delete ps;}private:string *ps;int i; }; HasPtr::HasPtr(const HasPtr & p):ps(new string(*p.ps)),i(p.i){}13.12
在下面的代碼片段中會發(fā)生幾次析構(gòu)函數(shù)的調(diào)用?
是3次,函數(shù)執(zhí)行完畢后銷毀局部對象accum,item1,item2共3個局部變量。
13.13
#include <memory> using namespace std; struct X{ X(int c):a(c){cout << "X(INT C)" << endl;} X(){cout << "X()" << endl;} X(const X&){cout << "X(const &X)" << endl;} X& operator=(const X&){cout << "X&operator=(const &X)" << endl;return *this;} ~X(){cout << "~X()" << endl;}private: int a = 0; int b; };int main() { //直接初始化 X a; X b(1234); X c(a); c = a;return 0; } X() X(INT C) X(const &X) X&operator=(const &X) ~X() ~X() ~X()13.1.4三/五法則
一、需要析構(gòu)函數(shù)的類也需要拷貝和賦值操作。
需要拷貝操作的類也需要賦值操作,反之亦然。
?
?
?
注:我們定義的析構(gòu)函數(shù)會釋放動態(tài)內(nèi)存,如不定義自己的析構(gòu)函數(shù),合成的析構(gòu)函數(shù)不會自動釋放動態(tài)內(nèi)存。
?
?
?
????????這記錄了需要拷貝構(gòu)造函數(shù)和拷貝賦值運算符但是不需要析構(gòu)函數(shù)的例子。具體程序如下。
練習(xí)題答案
練習(xí)13.1.4
答:這是一個典型的應(yīng)該定義拷貝控制成場合。如果不定義拷貝構(gòu)造函數(shù)和拷貝賦值運算符,依賴程序合成的版本,則在拷貝構(gòu)造和賦值時,會簡單賦值數(shù)據(jù)成員。對本問題來說,序簡單賦值給新對象。
? ? ? ? 因此,代碼中對a、b、c三個對象調(diào)用函數(shù),會輸出三個相同的序號-————合成拷貝構(gòu)造函數(shù)被調(diào)用時簡單復(fù)制序號,使得三個對象具有相同的序號。
結(jié)論:合成的默認(rèn)拷貝構(gòu)造函數(shù)和拷貝賦值運算符會直接拷貝所有的成員,不會成員的值。
? 練習(xí)13.15
答:在定義的時候,a、b、c獲得的mysn的值分別是0、1、2,但調(diào)用f的時候,還會進(jìn)行三次拷貝,第一次拷貝給函數(shù)f的變量,調(diào)用拷貝構(gòu)造函數(shù)mysn的值繼續(xù)增加1,變?yōu)?,以此類推,調(diào)用第二次mysn變?yōu)?、5,所以輸出結(jié)果是3、4、5。
當(dāng)然這假定第一個元素給值0的情況。
練習(xí)13.16.
答:如果f的參數(shù)是const Numbered &,那么輸出結(jié)果變?yōu)?,1,原因是如果參數(shù)類型傳遞的是const Numbered &,而不是類型本身,因為傳遞的是引用類型,所以并不會拷貝對象,所以輸出結(jié)果是0、1、2。
13.17。
13.14問題中的程序?qū)崿F(xiàn)如下:
#include <iostream> #include <memory> using namespace std; class Numbered; void f(Numbered); class Numbered{ friend void f(Numbered ); private:static int*ptr;int *p;int mysn; public:Numbered():p(ptr),mysn(*p){++ *p;}void delete_(){delete ptr;}};int * Numbered::ptr = new int(0); void f(Numbered s){cout << s.mysn << endl;}int main() { Numbered a,b = a ,c = b; f(a); f(b); f(c);//調(diào)用一個對象來銷毀static int *ptr 所指向的空間,當(dāng)然也把ptr定義為共有的成員,那樣就不用定義專門的成員函數(shù)了。但破壞了封裝的特性。 a.delete_(); return 0; }運行結(jié)果
0 0 014.15題的程序?qū)崿F(xiàn):
?
結(jié)果是
3
4
5
13.16實現(xiàn):
#include <iostream> #include <memory> using namespace std;class Numbered; void f(const Numbered &);class Numbered{friend void f(const Numbered &);private:static int*ptr;int *p;int mysn; public:Numbered():p(ptr),mysn(*p){++ *p;}Numbered(const Numbered &s){p = s.p;//從指針中取走內(nèi)存中的值mysn = *p;++ *p;}void delete_(){delete ptr;}};int * Numbered::ptr = new int(0); void f(const Numbered &s){cout << s.mysn << endl;}int main() { Numbered a,b = a ,c = b; f(a); f(b); f(c);//調(diào)用一個對象來銷毀static int *ptr 所指向的空間 a.delete_(); return 0; }結(jié)果是0? ?1? ?2
習(xí)題13.1.4解答完畢。
下面是以上用到程序的自己定義的版本,多定義了拷貝賦值運算符。
#include <string> #include <iostream> #include <memory> using namespace std; class Numbered{public://本程序需要定義一塊與類對象獨立的自由空間,用static可以達(dá)到這個目的static int * ptr;Numbered():p(ptr),mysn(*p){++ *p;}Numbered(const Numbered & s):p(s.p),mysn(*p){++ *p;}Numbered & operator=(const Numbered &s){p = s.p;mysn = *p;++ *p; //這里不能寫成*p ++,否則會先把p增加,然后解引用return *this;}int num()const{return mysn;}private:int *p;int mysn = 0;}; int * Numbered::ptr = new int(0); int main() { Numbered n1; cout << n1.num() << endl; Numbered n2 = n1; cout << n2.num() << endl; Numbered n3; cout << n3.num() << endl; n3 = n1; cout << n3.num() << endl; n1 = n3; cout << n1.num() << endl; Numbered n4; cout << n4.num() << endl; n4 = n2; cout << n4.num() << endl; Numbered n5; cout << n5.num() << endl; delete Numbered::ptr;return 0; } 0 1 2 3 4 5 6 7????????通過以上習(xí)題可以總結(jié)出一個結(jié)論,類需要使用的,但是不依賴類對象存在的事情,被定義為static是最合適的。這個對象是類使用的,但是和對象無關(guān),有關(guān)系。? ?
13.1.5?使用=default
13.1.6 阻止拷貝
?
//file.ccstruct NoCopy{ NoCopy() = default; NoCopy(const NoCopy &) = delete; NoCopy & operator=(const NoCopy &) = delete; ~NoCopy() = delete;private:int a = 0; };int main() { NoCopy n1,n2; //error: NoCopy n3 = n1; //error: n2 = n1; //error: return 0; }編譯后結(jié)果:?
file.cc: In function ‘int main()’: file.cc:14:8: error: use of deleted function ‘NoCopy::~NoCopy()’14 | NoCopy n1,n2;| ^~ file.cc:6:1: note: declared here6 | ~NoCopy() = delete;| ^ file.cc:14:8: error: use of deleted function ‘NoCopy::~NoCopy()’14 | NoCopy n1,n2;| ^~ file.cc:6:1: note: declared here6 | ~NoCopy() = delete;| ^ file.cc:14:11: error: use of deleted function ‘NoCopy::~NoCopy()’14 | NoCopy n1,n2;| ^~ file.cc:6:1: note: declared here6 | ~NoCopy() = delete;| ^ file.cc:14:11: error: use of deleted function ‘NoCopy::~NoCopy()’14 | NoCopy n1,n2;| ^~ file.cc:6:1: note: declared here6 | ~NoCopy() = delete;| ^ file.cc:16:6: error: use of deleted function ‘NoCopy& NoCopy::operator=(const NoCopy&)’16 | n2 = n1;| ^~ file.cc:5:10: note: declared here5 | NoCopy & operator=(const NoCopy &) = delete;| ^~~~~~~~=delete通知讀者和編譯器,我們不希望定義這些成員。
使用=delete要注意=delete和=default的差別:
1.=delete必須出現(xiàn)在函數(shù)第一次聲明的時候。一個默認(rèn)的成員只影響為這個成員而生成的代碼,因此,=default直到編譯器生成代碼的時候才需要。而另一方面,編譯器需要直到一個函數(shù)是刪除的,以便禁止試圖使用它的操作。所以必須第一次出現(xiàn)的時候就需要=delete,不然編譯器和用戶還以為是一般的函數(shù),到處使用。
2.與=default不同之處在于,=delete可以用在任何函數(shù)之中(我們只能對編譯器合成的默認(rèn)構(gòu)造函數(shù)或者拷貝構(gòu)造成員才能使用=default)。雖然刪除的函數(shù)主要用途是阻止禁止拷貝控制成員,但當(dāng)我們希望引導(dǎo)函數(shù)匹配的過程時,刪除函數(shù)有時也是有用的。
一、析構(gòu)函數(shù)不能是刪除的成員。
一個類或者它的成員的析構(gòu)函數(shù)都不能是刪除的,否則這個都無法創(chuàng)建這個類類型的對象。因為這個類型的對象無法刪除。下面是c++primer 5th的原文:
二、析構(gòu)函數(shù)是刪除的不能定義這個類型的成員,但是可以定義指向這個類型的指針,但是不能釋放這些對象。??????
自己編寫一遍試試:
#include <string> #include <iostream> #include <memory> using namespace std; class NoDtor{ public:NoDtor() = default; //顯示使用合成默認(rèn)析構(gòu)函數(shù)~NoDtor() = delete;//我們不能銷毀NoDtor類型的成員 }; int main() {NoDtor nd; //錯誤,析構(gòu)函數(shù)是刪除的,不能定義此類型對象NoDtor *p = new NoDtor(); // 正確,析構(gòu)函數(shù)是刪除的也可以動態(tài)分配這個類型的對象delete p; //錯誤,delete類類型對象會調(diào)用析構(gòu)函數(shù),但是析構(gòu)函數(shù)是刪除的 return 0; }?三、合成的拷貝控制成員可能是刪除的
1.如果類的某個類類型的成員的析構(gòu)函數(shù)是刪除的或者不可訪問的(例如private的),那么
這個類的合成的析構(gòu)函數(shù)就是刪除的。c++primer 5th原文:
2.如果類的某個成員的拷貝構(gòu)造函數(shù)是刪除的或者不可訪問的,那么這個類的合成的拷貝構(gòu)造函數(shù)就被定義為刪除的;如果一個成員析構(gòu)函數(shù)被定義為刪除的或者不可訪問的,那么這個類的合成的拷貝構(gòu)造函數(shù)也被定義為刪除的。c++primer 5th原文:
?3.如果類的某個成員的拷貝賦值運算符被定義為刪除的或者不可訪問的,或者是類的某個成員是const 或者 引用,那么這個類合成的拷貝賦值運算符就定義為刪除的。
class NoDtor{ public:NoDtor() = default; //顯示使用合成默認(rèn)析構(gòu)函數(shù)//我們不能銷毀NoDtor類型的成員 private:int &a; const int b; }; int main() {NoDtor n1;//因為類NoDtor有引用或者const成員,那么默認(rèn)構(gòu)造函數(shù)被定義為刪除的,所以不能創(chuàng)建這個類型對象 NoDtor n2; //error:原因同上 n2 = n1; //error:有const 或者引用類型(沒有合適的初始),拷貝賦值運算符定義為刪除的return 0; } ~4.如果類的某個成員的析構(gòu)函數(shù)是刪除的或者不可訪問的,那么類有一個引用成員,它沒有初始化器,或者類有一個const成員,它沒有類內(nèi)初始化器且類型未顯式定義默認(rèn)構(gòu)造函數(shù),則該類的默認(rèn)構(gòu)造函數(shù)被定義為刪除的。c++primer5th原文:
自己總結(jié)如下:
1.析構(gòu)函數(shù):如果一個類的析構(gòu)函數(shù)是delete的或者不可見的,那么會影響如下:
(1)該類的合成的默認(rèn)構(gòu)造函數(shù)是刪除的。不能定義此類型的對象。即使是調(diào)用此類型的合適的構(gòu)造函數(shù)也不行。
?(2)如果一個類的某個成員的析構(gòu)函數(shù)是刪除的或不可見的,那么這個類的合成的拷貝構(gòu)造函數(shù)就是定義刪除的。
??????? 其實也好理解,成員的析構(gòu)函數(shù)是刪除的或者不可見的,當(dāng)然也就不能定義那個成員類型了,當(dāng)然談不上那個成員的初始化了,那就這個類類型的合成拷貝構(gòu)造函數(shù)就自然被定義為刪除的了。按照常理推倒,即時自己定義拷貝構(gòu)造函數(shù)也不能調(diào)用去初始化這個類的對象,因為有個成員是不能定義的。
(3)如果一個類的某個成員的析構(gòu)函數(shù)是刪除的不可見的,那么這個類的合成的析構(gòu)函數(shù)也是刪除的。
提醒:析構(gòu)函數(shù)除了不影響拷貝賦值運算符,其余2個拷貝控制成員都影響,也影響默認(rèn)構(gòu)造函數(shù)。一共考慮四個函數(shù),default constructor、copy constructor、copy assignment operator、deconstructor,影響了3個。析構(gòu)函數(shù)真是夠刁的?;厥照竞苤匾?#xff0c;和現(xiàn)實世界一樣。
2.拷貝構(gòu)造函數(shù):如果一個類的拷貝構(gòu)造函數(shù)是delete的或者不可見的,那么會影響如下:
????????如果一個類的某個類類型成員拷貝構(gòu)造函數(shù)是刪除的或者不可見的,那么這個類類型的合成的拷貝構(gòu)造函數(shù)是刪除的或不可見的。
3.拷貝賦值運算符:如果一個類的拷貝賦值運算符是delete的或者不可見的,那么會影響如下:
??????? 如果一個類的某個成員的拷貝賦值運算符是刪除的或者不可見的,那么這個類類型的合成的拷貝賦值運算符就是刪除的或不可見的。
4.如果一個類有const或者引用成員(引用沒有初始化,const沒有初始值,或者const的類成員不能默認(rèn)構(gòu)造),那么會影響如下事情:
(1)首先這個類的合成的默認(rèn)構(gòu)造函數(shù)是刪除的。
(2)這個類的合成的拷貝賦值運算符被定義為刪除的。
?????? 也就是說,如果一個類有數(shù)據(jù)成員不能默認(rèn)構(gòu)造、拷貝、賦值或銷毀,那么對應(yīng)的成員函數(shù)將被定義為刪除的。補充就是析構(gòu)還會影響拷貝構(gòu)造和默認(rèn)構(gòu)造,以及const或者引用的也影響默認(rèn)構(gòu)造和拷貝賦值運算符的合成。
?注:給引用賦值以后,左邊引用會改變指向?qū)ο笾?#xff0c;而不會改變本身的值,賦值后,左側(cè)和右側(cè)引用指向不同的對象,這種行為看起來不是我們所期望嗯,所以,對于有引用類型的成員,我們定義為刪除的。
?四、private拷貝控制。
#include <string> #include <iostream> #include <memory> using namespace std; class Test{public:Test()= default;~Test()=default;private:Test(const Test &);Test& operator=(const Test &); }; int main() {Test t; //rightTest t1(t); //wrong:拷貝構(gòu)造函數(shù)是private的t1 = t; //wrong:拷貝賦值運算符是private的 return 0; } #include <string> #include <iostream> #include <memory> using namespace std; class Test{public:Test()= default;~Test()=default;Test copy(const Test &t){Test t1 = t; return t;}private:Test(const Test &);Test& operator=(const Test &); }; int main() {Test t;t.copy(t);return 0; }編譯時候錯誤(鏈接時候錯誤)是這樣的,如下:
/usr/bin/ld: /tmp/cckYppzr.o: in function `Test::copy(Test const&)': 3.cc:(.text._ZN4Test4copyERKS_[_ZN4Test4copyERKS_]+0x36): undefined reference to `Test::Test(Test const&)' /usr/bin/ld: 3.cc:(.text._ZN4Test4copyERKS_[_ZN4Test4copyERKS_]+0x49): undefined reference to `Test::Test(Test const&)' collect2: error: ld returned 1 exit status?13.1.6節(jié)練習(xí)答案:
練習(xí)13.18
答:
#include <string> #include <iostream> #include <memory> using namespace std; class Employee{private:static int cnt;string name;int number;public:Employee():number(cnt){++ cnt;}Employee(const string &s):name(s),number(cnt){++ cnt;}Employee(const Employee &s):name(s.name),number(cnt){++ cnt;}Employee & operator=(const Employee &s){name = s.name;number = cnt;++ cnt; return *this; }int num()const{cout << number << endl; return number;}}; int Employee::cnt = 0; int main() { Employee e1; e1.num(); Employee e2; e2.num(); Employee e3(e2); e3.num(); e1 = e3; e1.num();return 0; }?執(zhí)行結(jié)果:
0 1 2 3練習(xí)13.19
答案:
需要定義自己的拷貝控制成員。如果不定義的,那么直接拷貝號碼了,號碼一樣了。
練習(xí)13.20
答案:
TestQuery有2個數(shù)據(jù)成員,一個shared_ptr<vector<string>>類型的file,另一個map<string,shared_ptr<vector>>類型的wm成員。
TextQuery,拷貝時候,shared_ptr成員可以直接拷貝指針,左右兩邊指針都遞增引用計數(shù);map類型也是一樣,first成員直接拷貝,second成員直接拷貝指針,second成員指向的內(nèi)存的引用計數(shù)增加1。當(dāng)析構(gòu)的時候,分別調(diào)用2個類型的析構(gòu)函數(shù),自動釋放對象占有的內(nèi)存。
QueryResult類有3個數(shù)據(jù)成員,string sought、shared_ptr<set<line_so>> 類型的lines、shared_ptr<vector<string>>的file成員。
拷貝的時候,三個類型分別調(diào)用自己的拷貝構(gòu)造函數(shù),sought會直接拷貝,shared_ptr會直接拷貝指針,右側(cè)lines指向的內(nèi)存的引用計數(shù)增加1,file成員也一樣。析構(gòu)的時候分別調(diào)用的析構(gòu)函數(shù)。
練習(xí)13.21
答:不需要。理由如下:
兩個類雖然都沒有定義的拷貝控制成員,但是它們用智能指針管理共享的動態(tài)對象(輸入文件的內(nèi)容,查詢結(jié)果的行號集合),用標(biāo)準(zhǔn)庫容器保存大量的容器。而這些標(biāo)準(zhǔn)庫機制都有良好的拷貝控制成員,用合成的拷貝控制成員簡單地拷貝、賦值、銷毀它們,即可保證正確的資源管理。因此,這兩個類并不需要定義自己的拷貝控制成員。實際上,這兩個類的對象之間就存在資源共享,目前的設(shè)計能很好地實現(xiàn)這種共享,同類的對象之間的資源共享也能很自然地解決。
練習(xí)13.22
答:
//文件HasPtr.cpp #include "HasPtr.h" #include <string> #include <iostream> #include <new> using namespace std; HasPtr& HasPtr::operator= (const HasPtr& s) {string * new_ps = new string(*s.ps);//釋放掉ps原來指向的內(nèi)存delete ps;i = s.i;ps = new_ps;return *this; }HasPtr::HasPtr(const HasPtr &s):ps(new string(*s.ps)),i(s.i){}HasPtr::~HasPtr(){ delete ps; } //文件HasPtr.h #pragma once #include <string> #include <new> #include <iostream> using namespace std; class HasPtr { public:HasPtr(const string& s = string()):ps(new string(s)),i(0) {}HasPtr& operator= (const HasPtr& h);HasPtr(const HasPtr&);~HasPtr(); private:string* ps;int i; }; //文件main.cpp #include <string> #include <iostream> #include <new> #include "HasPtr.h" using namespace std; int main() {return 0; }練習(xí)13.23
答:差異就是自己沒考慮到自復(fù)制時候的情況。自己的代碼沒考慮到自復(fù)制,無法處理自賦值。
練習(xí)13.24
答:如果沒定義析構(gòu)函數(shù),那么程序結(jié)束的時候會發(fā)生內(nèi)存泄漏,如果沒有定義拷貝構(gòu)造函數(shù),那么系統(tǒng)會使用默認(rèn)拷貝構(gòu)造函數(shù),會直接拷貝指針。
練習(xí)13.25
答:
//main.cpp #include <string> #include <iostream> #include <vector> #include <new> #include <memory> #include "StrBlob.h" using namespace std; int main() { StrBlob s1({"st1","st2","st3","st4"}); cout << s1.back() << endl; StrBlob s2 = s1; s1.pop_back(); cout << s1.size() << endl; cout << s2.size() << endl; cout << s1.back() << endl; cout << s2.back() <<endl;return 0; } //StrBlob.cpp #include "StrBlob.h" #include <string> //string #include <vector> //vector #include <new> //new #include <memory> //memory #include <stdexcept> //out_of_range using namespace std; StrBlob::StrBlob():data(make_shared<vector<string>>()) {} StrBlob::StrBlob(initializer_list<string> il) : data(make_shared<vector<string>>(il)) {}StrBlob::StrBlob(const StrBlob& s) : data(make_shared<vector<string>>(*s.data)){ } StrBlob::~StrBlob() {} StrBlob& StrBlob::operator=(const StrBlob& s) {data = make_shared<vector<string>>(*s.data);return *this; }void StrBlob::push_back(const string & s) {data->push_back(s); } void StrBlob::pop_back() {check(0,"pop_back on empty StrBlob.");data->pop_back(); }string& StrBlob::front() {return data->front(); } string& StrBlob::back() {check(0,"back on empty StrBlob."); return data->back(); }void StrBlob::check(size_type i,const string & msg) const {if (i >= data->size())throw out_of_range(msg); } #pragma once #include <vector> #include <string> #include <new> #include <memory> using namespace std; class StrBlob { public:typedef vector<string>::size_type size_type;StrBlob();StrBlob(initializer_list<string> il);StrBlob(const StrBlob&);StrBlob& operator=(const StrBlob&);~StrBlob();size_type size()const { return data->size(); }bool empty()const { return data->empty(); }//add and delete elementvoid push_back(const string& t);void pop_back();//element accessstring& front();string& back(); private:shared_ptr<vector<string>> data;void check(size_type i,const string & msg)const;};13.25.
答:拷貝構(gòu)造函數(shù)和拷貝賦值運算符應(yīng)該為自身分配動態(tài)內(nèi)存,而不是與右側(cè)操作數(shù)共享對象。
StrBlob使用的是智能指針,可以用合成析構(gòu)函數(shù)來管理,如果一個StrBlob對象離開其作用域,則會自動調(diào)用std::shared_ptr的析構(gòu)函數(shù),當(dāng)use_count為0時釋放動態(tài)分配的內(nèi)存。
13.26
答:
//StrBlob.h #include <string> #include <iostream> #include <memory> #include <vector> #include <new> using namespace std; class StrBlob{public:using size_type = vector<string>::size_type;StrBlob();StrBlob(initializer_list<string> il);string &front()const;string &back()const;void pop_back();void push_back(const string &s);//copy constructorStrBlob(const StrBlob &s);//copy assignment operatorStrBlob& operator=(const StrBlob &s);//deconstructor ~StrBlob();private:shared_ptr<vector<string>> data;void check(size_type i,const string & msg)const; }; //functions.cc #include <string> #include <iostream> #include <memory> #include <vector> #include <new> #include "StrBlob.h" using namespace std; StrBlob::StrBlob():data(make_shared<vector<string>>()){} StrBlob::StrBlob(initializer_list<string> il):data(make_shared<vector<string>>(il)){}//copy constructor StrBlob::StrBlob(const StrBlob &s):data(make_shared<vector<string>>(*s.data)){} //copy assignment operator StrBlob& StrBlob::operator=(const StrBlob &s) {data = make_shared<vector<string>>(*s.data);return *this; } //deconstructor StrBlob::~StrBlob(){}string& StrBlob::front()const {return data->front(); } string& StrBlob::back()const {return data->back(); }void StrBlob::pop_back() {data->pop_back(); } void StrBlob::push_back(const string &s) {data->push_back(s); }void StrBlob::check(size_type i,const string &msg)const { if(i >= data -> size())throw out_of_range(msg); } #include <string> #include <iostream> #include <memory> #include <vector> using namespace std; int main() {return 0; }13.2.2
本節(jié)內(nèi)容主要涉及實現(xiàn)類指針的類
//functions.cc #include "hasptr.h" #include <string> #include <iostream> #include <memory> #include <new> using namespace std; HasPtr::HasPtr(const string &s):ps(new string(s)),use(new size_t(0)),i(0) { //++ is priority than *(*use) ++; //or can be written ++*use } HasPtr::HasPtr(const HasPtr &rhs):ps(rhs.ps),i(rhs.i),use(rhs.use) { (*use) ++; }HasPtr& HasPtr::operator=(const HasPtr &rhs) { //自己賦值給自己什么都不發(fā)生 ++ *rhs.use;//遞增右側(cè)運算對象的引用計數(shù) if(--*use == 0){delete ps;delete use;} i = rhs.i; use = rhs.use; ps = rhs.ps;return *this; }HasPtr::~HasPtr() { //析構(gòu)函數(shù)會遞減引用計數(shù),如果引用計數(shù)變?yōu)?,那么銷毀該對象以及該對象所占用的資源 if(--*use == 0){delete ps;delete use;} }//hasptr.h #include <string> #include <iostream> #include <memory> #include <new> #include <vector> using namespace std; class HasPtr{public:HasPtr(const string &s = string());HasPtr(const HasPtr &rhs);HasPtr& operator=(const HasPtr &rhs);~HasPtr();size_t use_count()const{return *use;}private:string *ps;int i;size_t *use; };//main.cc #include <string> #include <iostream> #include <memory> #include <vector> #include <new> #include "hasptr.h" using namespace std; int main() {HasPtr h1;cout << (h1.use_count()) << endl;HasPtr h2 = h1;cout << h1.use_count() << endl;cout << h2.use_count() << endl;HasPtr h3;cout << h3.use_count() << endl;h3 = h2;h2 = h2;cout << h3.use_count() << endl;cout << h2.use_count() << endl;cout << h1.use_count() << endl;return 0; }這里主要有這么幾個問題,
首先:引用計數(shù)應(yīng)該用動態(tài)對象去記錄。
其次:類指針的類在幾個構(gòu)造函數(shù)或者析構(gòu)函數(shù)的設(shè)計注意如下問題:
1.拷貝構(gòu)造函數(shù):
對于引用計數(shù),先從右側(cè)對象拷貝過來,然后要遞增引用計數(shù)。其余成員逐個拷貝。就相當(dāng)于:
先把所有成員都拷貝過來(包括動態(tài)內(nèi)存成員),然后再把動態(tài)成員中的引用計數(shù)自增即可。
2.拷貝賦值運算符:
????????要正確處理自賦值情況,先遞增右側(cè)引用計數(shù),再遞減左側(cè)引用計數(shù)??截愘x值運算符進(jìn)行構(gòu)造函數(shù)和析構(gòu)函數(shù)的雙重任務(wù),在左側(cè)引用計數(shù)變?yōu)?的情況下,執(zhí)行析構(gòu)函數(shù)部分的工作(銷毀左側(cè)對象)。
3.析構(gòu)函數(shù):
? ? ? ? 析構(gòu)函數(shù)遞減引用計數(shù),如果左側(cè)的引用計數(shù)變?yōu)?,那么銷毀對象。
13.2.2節(jié)練習(xí)
練習(xí)13.27:定義你自己的引用計數(shù)版本的HasPtr。
答:略
練習(xí)13.28
答:略
13.3節(jié)
13.29
答:解釋swap(HasPtr &,HasPtr &)中對swap的調(diào)用會不會導(dǎo)致遞歸循環(huán)。
在此swap函數(shù)中又調(diào)用了swap來交換HasPtr成員ps和i。但這兩個成員的類型分別為指針和整型,都是內(nèi)置類型,因此函數(shù)中的swap調(diào)用被解析為std::swap,而不HasPtr的特定版本的swap(也就是自身),所以不會導(dǎo)致遞歸循環(huán)。
練習(xí)13.30
為你的類值版本的HasPtr編寫swap函數(shù),并測試它。為你的swap函數(shù)添加一個打印語句,指出函數(shù)什么時候執(zhí)行。
答:里面有三個文件,即是答案
//main.cc #include <string> #include <new> #include <iostream> #include "hasptr.h" using namespace std; int main() { HasPtr h1("hello,china"),h2("hello,world!"); swap(h1,h2);return 0; }//functions.cc #include "hasptr.h" #include <string> #include <iostream> #include <memory> using namespace std; HasPtr::HasPtr(const string &s):ps(new string(s)){} HasPtr::HasPtr(const HasPtr &s):ps(new string(*s.ps)),i(s.i){} HasPtr& HasPtr::operator=(const HasPtr &s){ string *new_ps = new string(*s.ps); delete ps; ps = new_ps; i = s.i;return *this; } HasPtr::~HasPtr() {delete ps; }void swap(HasPtr & lhs,HasPtr & rhs) { using std::swap; swap(lhs.ps,rhs.ps); swap(lhs.i ,rhs.i); cout << "swap(HasPtr &,HasPtr & )" << endl; }//hasptr.h #include <string> #include <iostream> #include <new> using namespace std; //類值版本 class HasPtr{public:friend void swap(HasPtr &,HasPtr &);HasPtr(const string &s = string());HasPtr(const HasPtr &);HasPtr& operator=(const HasPtr &);~HasPtr();size_t use_count()const;private:string *ps;int i; };void swap(HasPtr &,HasPtr &);運行結(jié)果:
swap(HasPtr &,HasPtr & )13.31
解:
//main.cc #include <string> #include <new> #include <iostream> #include "hasptr.h" #include <vector> #include <algorithm> using namespace std; int main() { HasPtr h1("hello,china"),h2("hello,world!"),h3("hello,award.."); HasPtr h4("bc"),h5("cde"),h6("fgh"); HasPtr h7("efasdf"),h8("hiasdff"),h9("ijiasdf"); HasPtr h10("jsdf"),h11("kjk"),h12("lsdff"); HasPtr h13("msdf"),h14("nsdf"),h15("osdf"); HasPtr h16("psdf"),h17("qasf"),h18("rsfd");swap(h1,h2); vector<HasPtr> vs; vs.push_back(h1); vs.push_back(h2); vs.push_back(h3); vs.push_back(h4); vs.push_back(h5); vs.push_back(h6); vs.push_back(h7); vs.push_back(h8); vs.push_back(h9); vs.push_back(h10); vs.push_back(h11); vs.push_back(h12); vs.push_back(h13); vs.push_back(h14); vs.push_back(h15); vs.push_back(h16); vs.push_back(h17); vs.push_back(h18); sort(vs.begin(),vs.end()); for(auto i : vs)cout << i.str() << endl;return 0; }//functions.cc #include "hasptr.h" #include <string> #include <iostream> #include <memory> using namespace std; HasPtr::HasPtr(const string &s):ps(new string(s)),i(0){} HasPtr::HasPtr(const HasPtr &s):ps(new string(*s.ps)),i(s.i){} HasPtr& HasPtr::operator=(const HasPtr &s){ string *new_ps = new string(*s.ps); delete ps; ps = new_ps; i = s.i;return *this; } HasPtr::~HasPtr() {delete ps; }void swap(HasPtr & lhs,HasPtr & rhs) { using std::swap; swap(lhs.ps,rhs.ps); swap(lhs.i ,rhs.i); cout << "swap(HasPtr &,HasPtr & )" << endl; }bool HasPtr::operator<(const HasPtr &rhs)const { cout << "opeartor<" << endl; return (*ps < *rhs.ps); }//hasptr.h #include <string> #include <iostream> #include <new> using namespace std; //類值版本 class HasPtr{public:friend void swap(HasPtr &,HasPtr &);HasPtr(const string &s = string());HasPtr(const HasPtr &);HasPtr& operator=(const HasPtr &);bool operator<(const HasPtr &)const;string str()const{return *ps;}~HasPtr();size_t use_count()const;private:string *ps;int i; };void swap(HasPtr &,HasPtr &);運行結(jié)果:
swap(HasPtr &,HasPtr & ) opeartor< opeartor< swap(HasPtr &,HasPtr & ) opeartor< opeartor< opeartor< opeartor< opeartor< opeartor< opeartor< opeartor< opeartor< opeartor< opeartor< opeartor< opeartor< opeartor< opeartor< opeartor< opeartor< opeartor< opeartor< opeartor< opeartor< opeartor< opeartor< opeartor< opeartor< opeartor< opeartor< opeartor< opeartor< opeartor< opeartor< opeartor< opeartor< opeartor< opeartor< opeartor< opeartor< opeartor< opeartor< opeartor< opeartor< opeartor< opeartor< opeartor< opeartor< opeartor< opeartor< opeartor< opeartor< opeartor< opeartor< opeartor< opeartor< opeartor< opeartor< opeartor< opeartor< opeartor< opeartor< opeartor< opeartor< opeartor< opeartor< bc cde efasdf fgh hello,award.. hello,china hello,world! hiasdff ijiasdf jsdf kjk lsdff msdf nsdf osdf psdf qasf rsfd說明,一開始弄個3個元素(vector的元素數(shù)),發(fā)現(xiàn)結(jié)果沒有調(diào)用swap,后來是18個元素,發(fā)現(xiàn)調(diào)用了一此swap。說明當(dāng)元素數(shù)量比較少的時候,sort用的是其它方式,而元素數(shù)量比較多的時候,用的swap交換元素。
13.32
答:默認(rèn)的swap版本交換兩個對象的非靜態(tài)成員,對HasPtr而言,就是交換string 的指針ps,引用計數(shù)指針use和整型數(shù)值i??梢钥闯?#xff0c;這種語意是符合期望的——兩個HasPtr指向了原來對方的string,兩者互換string后,各自的引用計數(shù)本應(yīng)該是不變的(都是減1后加1)。因此,默認(rèn)swap版本已經(jīng)能正確處理類指針HasPtr的交換,專用swap版本不會帶來更多的收益。
//strvec.cc#include <string> #include <iostream> #include <memory> #include "strvec.h" using namespace std;//一個類對象是const的,它的元素不是const的 StrVec::StrVec(const StrVec &rhs) { //allocate_n_copy:可以開辟新的存儲空間,然后把給定參數(shù)范圍的元素逐個拷貝進(jìn)去,然后 //返回構(gòu)造好的空間的首元素地址和尾后元素的地址 pair<string*, string*> data = allocate_n_copy(rhs.elements,rhs.first_free); elements = data.first; first_free = cap = data.second; } StrVec& StrVec::operator=(const StrVec & rhs) {auto data = allocate_n_copy(rhs.begin(),rhs.end());free(); //拷貝賦值運算符比拷貝構(gòu)造函數(shù)多一個釋放左側(cè)對象的空間的函數(shù)elements = data.first;first_free = cap = data.second;return *this; } size_t StrVec::capacity()const {return cap - elements; } size_t StrVec::size()const {return first_free - elements; }void StrVec::free() { if(elements){for(string * p = first_free; p != elements;){alloc.destroy(-- p); }alloc.deallocate(elements,cap - elements); //cap -elements 可以計算一共有多少空間} }void StrVec::chk_n_alloc() { //如果容器已滿,那么重新分配 if(size() == capacity())reallocate(); }pair<string*,string*> StrVec::allocate_n_copy(const string* b,const string * e) {string * const data = alloc.allocate(e - b);return {data,uninitialized_copy(b,e,data)};} allocator<string> StrVec::alloc;void StrVec::reallocate_1() { size_t new_capacity = size() ? 1 : 2 * size(); auto new_elements = alloc.allocate(new_capacity); auto new_first_free = uninitialized_copy(elements,first_free,new_elements); free(); auto new_cap = new_elements + new_capacity; elements = new_elements; first_free = new_first_free; cap = new_cap; }void StrVec::reallocate() {auto new_capacity = size() ? 2*size() : 1; auto new_elements = alloc.allocate(new_capacity); auto dest = new_elements; auto elem = elements; for(int i = 0; i != size() ; ++ i) {//std::move(),在需要構(gòu)造的地方也是可以使用的 alloc.construct(dest ++,std::move(*elem++)); } free(); elements = new_elements; first_free = dest; cap = new_elements + new_capacity;}void StrVec::push_back(const string & rhs) {chk_n_alloc(); //確保有空間容納新元素alloc.construct(first_free ++,10,'c');}void StrVec::pop_back() {alloc.destroy(first_free --); }StrVec::~StrVec() {free(); }string * StrVec::begin()const {return elements; } string * StrVec::end()const {return first_free; }void StrVec::reserve(size_t s) { if(s <= size()) return ;//先移動元素,然后再釋放左側(cè)對象空間 auto const new_elements = alloc.allocate(s); auto dest = new_elements; auto elem = elements; for(size_t i = 0; i != size();++ i ) {alloc.construct(dest ++, std::move(*elem ++));} free(); elements = new_elements; cap = new_elements + s; first_free = dest;}void StrVec::resize(size_t t) { if(t > capacity())return ; else if(t < size()){auto new_first_free = first_free;for(size_t i = 0; i != size() - t; ++ i){alloc.destroy(-- new_first_free);}first_free = new_first_free;return;} else if(t == size()){return ;} else if(t > size()){auto new_first_free = first_free;for(size_t i = 0;i != t - size() ; ++ i){alloc.construct(new_first_free ++,""); } first_free = new_first_free; return;} }void StrVec::print_info()const { cout << "capacity:" << capacity() << endl; cout << "size:" <<size()<< endl; }文件main.cc
#include <string> #include <iostream> #include <memory> #include "strvec.h" using namespace std; int main() { StrVec v; //cout << v.capacity() << endl; //cout << v.size() << endl;for(int i = 0;i != 10; ++i){v.push_back(string("abcde"));cout << v.size() << endl;} cout <<"capacity:" << v.capacity() << endl; cout <<"size:" << v.size() << endl;for(string * p = v.begin(); p != v.end(); ++p)cout << *p << endl; v.print_info();v.resize(5); v.resize(5); v.resize(100); cout << "big" << endl; v.print_info(); v.reserve(100); v.print_info(); cout << "after resize: " << endl; for(string* p = v.begin(); p != v.end(); ++ p)cout << *p << endl; v.print_info();return 0; } //strvec.h #include <string> #include <iostream> #include <memory> using namespace std; class StrVec {public:StrVec(): //allocator成員進(jìn)行默認(rèn)初始化elements(nullptr),first_free(nullptr),cap(nullptr){}StrVec(const StrVec &); //拷貝構(gòu)造函數(shù)StrVec& operator=(const StrVec &);~StrVec();void push_back(const string &);void pop_back();size_t size()const;size_t capacity()const;string *begin()const;string *end()const;//習(xí)題13.39void resize(size_t );void reserve(size_t );void shrink_to_fit();void print_info()const;private:static allocator<string> alloc;string *elements; //指向指向數(shù)組首元素的指針string *first_free; //指向第一個空閑元素的指針string *cap; //指向數(shù)組尾后位置的指針void free(); //銷毀元素并且釋放內(nèi)存void reallocate_1(); 獲得更多內(nèi)存并拷貝已有元素void reallocate();pair<string*,string*> allocate_n_copy(const string*,const string*);//開辟空間,拷貝給定范圍的元素void chk_n_alloc();};運行結(jié)果:?
1 2 3 4 5 6 7 8 9 10 capacity:16 size:10 cccccccccc cccccccccc cccccccccc cccccccccc cccccccccc cccccccccc cccccccccc cccccccccc cccccccccc cccccccccc capacity:16 size:10 big capacity:16 size:5 capacity:100 size:5 after resize: cccccccccc cccccccccc cccccccccc cccccccccc cccccccccc capacity:100 size:5strvec.h中雖然申明了shrink_to_fit,但是這個函數(shù)目前實現(xiàn)不了,因為allocator對象分配的空間,目前不能釋放一部分;因為釋放allocator對象分配的空間需要函數(shù)deallocate函數(shù),這個函數(shù)形如:
deallocate(p,n);其中p必須是先前調(diào)用allocate得到的指針,而且n必須是先前調(diào)用allocate時候的空間的大小。
所以只能全部釋放呀。不能部分釋放。所以shrink_to_fit是不能實現(xiàn)的。
習(xí)題13.40自編答案
StrVec::StrVec(initializer_list<string> li) { //迭代器可以直接給指針賦值,棒呀,都什么情況可以這樣操作呢? auto data = allocate_n_copy(li.begin(),li.end()); elements = data.first; first_free = cap = data.second; }練習(xí)13.42
//functions.cc #include <string> #include <iostream> #include "strblob.h" #include "strblobptr.h" #include <vector> #include "strvec.h" using namespace std;//strblob.h成員函數(shù)實現(xiàn) StrBlob::StrBlob():data(make_shared<StrVec>()){} StrBlob::StrBlob(const StrVec &s):data(make_shared<StrVec>(s)){}StrBlob::size_type StrBlob::size()const{return data->size(); } void StrBlob::push_back(const string &ele) {data->push_back(ele); }void StrBlob::pop_back(){data->pop_back(); } string & StrBlob::front() { return data->front();} string & StrBlob::back() { return data->back(); } const string& StrBlob::front()const{return data->front(); } const string& StrBlob::back()const{return data->back(); }bool StrBlob::empty()const { return data->empty(); }/* following is the member functions of class StrBlobPtr */ StrBlobPtr::StrBlobPtr():curr(0){} StrBlobPtr::StrBlobPtr(StrBlob b,size_t i):wptr(b.data),curr(i){}shared_ptr<StrVec> StrBlobPtr::check(size_t i,const string &msg)const{shared_ptr<StrVec> ret = wptr.lock(); //question1:什么情況下,ret會不指向任何對象呢? if(!ret)throw runtime_error("unbounded StrBlobPtr"); else if(i >= ret->size())throw out_of_range(msg); elsereturn ret; }StrBlobPtr StrBlob::begin()const { return StrBlobPtr(*this); } StrBlobPtr StrBlob::end()const { return StrBlobPtr(*this,this->size()); }string & StrBlobPtr::deref()const { auto ret = check(curr,"out of range"); return *(ret->begin() + curr); }StrBlobPtr &StrBlobPtr::incr() {check(curr,"out of range");++ curr;return *this; }//strvec.h StrVec::StrVec():elements(nullptr),first_free(nullptr),cap(nullptr){} allocator<string> StrVec::alloc; string &StrVec::front()const { return *elements;} string & StrVec::back()const { return *(first_free - 1);} string *StrVec::begin()const { return elements; } string * StrVec::end()const { return first_free; }StrVec::StrVec(const StrVec &s) { auto data = allocate_n_copy(s.begin(),s.end()); elements = data.first; cap = first_free = data.second; } StrVec& StrVec::operator=(const StrVec& s) { auto data = allocate_n_copy(s.begin(),s.end()); free(); elements = data.first; cap = first_free = data.second;return *this; }pair<string*,string*> StrVec::allocate_n_copy(const string* b,const string *e) { string * new_elements = alloc.allocate(e - b); string * dest = new_elements; const string * elem = b; while(elem != e){alloc.construct(dest ++,std::move(*elem ++)); }return {new_elements,dest}; }void StrVec::free() { auto elem = elements; while(elem != first_free){alloc.destroy(elem ++);} alloc.deallocate(elements,cap - elements); } size_t StrVec::size()const { return (first_free - elements);} void StrVec::push_back(const string &s) { if(first_free == cap)reallocate(); alloc.construct(first_free ++ ,s); } void StrVec::pop_back() { check(0,"pop on empty StrVec."); alloc.destroy(--first_free); } bool StrVec::empty()const { if(!elements)return true; elsereturn false;} StrVec::~StrVec() { free(); }void StrVec::reallocate() { size_t new_size = size()? 2*size():1; auto new_elements = alloc.allocate(new_size); auto new_first_free = uninitialized_copy(elements,first_free,new_elements); free(); elements = new_elements; first_free = new_first_free; cap = new_elements + new_size; }void StrVec::check(size_t i,const string &msg)const { if(i >= size()) throw out_of_range(msg); }bool StrBlobPtr::operator!=(const StrBlobPtr &s) {auto p = this->wptr.lock();auto q = s.wptr.lock();if(!(p->elements == q->elements && p->first_free == q->first_free && p->cap == q->cap))return true;elsereturn false; } //main.cc#include <string> #include <iostream> #include "strblob.h" #include "strblobptr.h" #include <vector> #include "strvec.h" using namespace std; int main() { StrVec s1; s1.push_back("abc"); s1.push_back("def"); s1.push_back("fgh"); s1.push_back("qhk"); //for(string* ptr = s1.begin(); ptr != s1.end() ;++ ptr) // cout << *ptr << endl; for(auto p = s1.begin(); p != s1.end() ; ++p)cout << *p << endl;StrBlob str(s1); StrBlobPtr pk(str); pk.incr();cout << "deref :" << endl; cout << pk.deref() << endl; cout << "end ." << endl; cout << str.size() << endl; cout << str.empty() << endl; cout << str.front() << endl; cout << str.back() << endl;return 0; } //strblob.h #ifndef STRBLOB_H #define STRBLOB_H#include <iostream> #include <string> #include <memory> #include <vector> #include "strvec.h" using namespace std; class StrBlobPtr; class StrBlob{public:friend class StrBlobPtr;StrBlob();StrBlob(const StrVec &l);using size_type = vector<string>::size_type;size_type size()const;void push_back(const string &ele);void pop_back();string &front();string &back();const string &front()const;const string &back()const;bool empty()const;StrBlobPtr begin()const;StrBlobPtr end()const;private:shared_ptr<StrVec> data;void check(size_type i,const string &msg);};#endif //strblobptr.h#ifndef INCLUDE_H_STRBLOBPTR #define INCLUDE_H_STRBLOBPTR#include <string> #include <iostream> #include <memory> #include <vector> #include "strvec.h" using namespace std; class StrBlobPtr{public: StrBlobPtr(); StrBlobPtr(StrBlob b,size_t curr = 0); string & deref()const; StrBlobPtr & incr(); bool operator!=(const StrBlobPtr &s);private:weak_ptr<StrVec> wptr;size_t curr;shared_ptr<StrVec> check(size_t i,const string &msg)const; };#endif //strvec.h#ifndef STRVEC_H #define STRVEC_H#include <string> #include <iostream> #include <memory> using namespace std; class StrVec {friend class StrBlobPtr;public:StrVec();StrVec(const StrVec &);StrVec& operator=(const StrVec &);~StrVec();string & front()const;string & back()const;string * begin()const;string * end()const;void pop_back();void push_back(const string &s);size_t size()const;bool empty()const;void reallocate();private:static allocator<string> alloc;string * elements;string * first_free;string * cap;pair<string*,string*> allocate_n_copy(const string *,const string *);void free();//主要檢查是否i已經(jīng)達(dá)到容器的尾后位置void check(size_t i,const string &msg)const; };#endif在g++編譯器中執(zhí)行:
g++ main.cc functions.cc -o exe ./exe得到如下結(jié)果:
abc def fgh qhk deref : def end . 4 0 abc qhk結(jié)果,目前來來,全部通過編譯。
13.43更傾向于for_each算法,因為不用for去迭代。
13.44
//string.h #ifndef STRING_H_INCLUDE #define STRING_H_INCLUDE#include <utility> #include <string> #include <memory> using namespace std; class String {public:String();char * begin()const;char * end()const;char front()const;char back()const;size_t size()const;bool empty()const;void push_back(char ch);void pop_back();size_t capacity()const; private:void check(size_t ,const string &msg)const;pair<char*,char*>allocate_n_copy(char* ,char *);void free();char * elements;char * first_free;char * cap;void reallocate();static allocator<char> alloc;};#endif //functions.cc#include <iostream> #include <string> #include "string.h" #include <memory> #include <algorithm> using namespace std;String::String():elements(nullptr),first_free(nullptr),cap(nullptr){} allocator<char> String::alloc; char* String::begin()const {return elements; }char* String::end()const {return first_free; }void String::check(size_t i,const string &msg)const { if(i >= size())throw out_of_range(msg); }size_t String::size()const { return first_free - elements; } bool String::empty()const { if(size() == 0)return true; elsereturn false; } void String::push_back(char ch) { if(size() == capacity())reallocate();alloc.construct(first_free ++,ch); } void String::pop_back() {check(0,"pop on empty String.");alloc.destroy(--first_free); }char String::front()const { check(0,"front on empty String.");return *elements; }char String::back()const { check(0,"back on empty String."); char * ptr = first_free - 1;return *ptr; }size_t String::capacity()const {return cap - elements; } void String::free() /*這里一定要注意,考慮元素個數(shù)是0個的情況*/ { if(!elements)return ; for_each(elements,first_free,[](char ch){alloc.destroy(&ch);});//for_each算法省去了循環(huán) alloc.deallocate(elements,capacity()); }pair<char*,char*> String::allocate_n_copy(char * b,char * e) { auto new_elements = alloc.allocate(e - b);return {new_elements,uninitialized_copy(b,e,new_elements)}; }void String::reallocate() { auto new_size = size()?2 *size() : 1; auto new_elements = alloc.allocate(new_size); auto new_first_free = uninitialized_copy(elements,first_free,new_elements); free(); elements = new_elements; first_free = new_first_free; cap = new_size + new_elements; } //main.cc #include <string> #include <iostream> #include <memory> #include "string.h" using namespace std;int main() { String s; s.push_back('a'); cout << s.front() << endl; cout << s.back() << endl;return 0; }13.6.2節(jié)學(xué)習(xí)
類似string類(以及其他標(biāo)準(zhǔn)庫類),如果我們自己的類能夠同時支持移動和拷貝,那么也能從中受益。為了讓我們自己的類型同時支持移動和拷貝,那么我們需要定義自己的移動構(gòu)造函數(shù)和移動賦值運算符。這兩個成員類似拷貝構(gòu)造函數(shù)和拷貝賦值運算符,但是它們是從給定對象“竊取”資源而不是拷貝資源。
? ? ? ? 類似拷貝構(gòu)造函數(shù),移動構(gòu)造函數(shù)的第一個參數(shù)也是該類類型的一個引用。不同于拷貝構(gòu)造函數(shù)的是,這個引用參數(shù)在移動構(gòu)造函數(shù)中是一個右值引用。與拷貝構(gòu)造函數(shù)一樣,任何其他的參數(shù)必須有默認(rèn)實參。
習(xí)題13.6.2
習(xí)題13.49
類StrVec的移動構(gòu)造函數(shù)和移動賦值運算符:
//(1)接管資源后,要把被接管資源置于安全狀態(tài)(可析構(gòu)的狀態(tài):容器clear,內(nèi)置指針nullptr,allocator //先destroy元素,然后deallocator) //(2)注意代碼書寫也不一樣 StrVec::StrVec(StrVec && v)noexcept:elements(v.elements),first_free(v.first_free),cap(s.cap) { v.elements = v.first_free = v.cap = nullptr; } //1.(代碼從參數(shù)列表開始就注意了) //2.判斷是否是左右操作數(shù)是同一個對象,防止自賦值情況 //3.要把被接管或者被移動的對象置于安全狀態(tài)(可以析構(gòu)的狀態(tài)) StrVec& StrVec::operator=(StrVec && v)noexcept {if(this != &v){free();elements = v.elements;first_free = v.first_free;cap = v.cap;v.elements = v.first_free = v.cap = nullptr; }return *this; }String.h的?
String::String(String && s)noexcept:elements(s.elements),first_free(s.first_free),cap(s.cap) {s.elements = s.first_free = s.cap = nullptr; }String &String::operator=(String&& s):noexcept { if(this != &s){free();elements = s.elements;first_free = s.first_free;cap = s.cap;s.elements = s.first_free = s.cap = nullptr;}return *this; } Message::Message(Message && rhs):contents(std::move(rhs.contents)) {move_Folders(&rhs); } Message& Message::operator=(Message && m) { if(this != &m){remove_from_Folders();contents = std::move(m.contents);move_Floders(&m); } return *this; }
注意:接管資源后,要把被接管資源置于安全狀態(tài)(可析構(gòu)的狀態(tài):容器clear,內(nèi)置指針nullptr,allocator對象先destroy元素,然后deallocator。
hasptr類指針版本
文件hasptr.h
#ifndef HASPTR_H #define HASPTR_H#include <string> #include <iostream> #include <new> using namespace std; class HasPtr{public:HasPtr(const string &s = string());HasPtr(const HasPtr &);HasPtr& operator=(const HasPtr &);HasPtr(HasPtr &&)noexcept;HasPtr& operator=(HasPtr &&)noexcept;~HasPtr();size_t use_count()const{cout <<"use_count:" << *use << endl;return *use;}private:size_t * use;string * ps;int i; };inline HasPtr::HasPtr(const std::string &s): ps(new std::string(s)), i(0), use(new std::size_t(1)) {}// copy constructor copies all three data members // and increments the counter inline HasPtr::HasPtr(const HasPtr &p): ps(p.ps), i(p.i), use(p.use) { ++*use; }inline HasPtr::HasPtr(HasPtr &&p)noexcept: ps(p.ps), i(p.i), use(p.use) { p.ps = 0; p.use = 0; }inline HasPtr::~HasPtr() { //you must charge whether the object is released before //f.g.it can operate delete use and delete ps,so it is released before,it can't released again at all if(!use) //important{return ;}if (--*use == 0) { // if the reference count goes to 0delete ps; delete use; use = nullptr; //it'll be a minute can determine that whether it is released ps = nullptr; } }inline HasPtr & HasPtr::operator=(HasPtr &&rhs)noexcept {//這里先判斷是不是自賦值情況,然后才能做賦值運算,如果先做賦值運算,那么釋放的對象就不對了if (this != &rhs) {if (--*use == 0) { // do the work of the destructordelete ps;ps = nullptr;delete use;use = nullptr;//無論什么時候,內(nèi)置指針釋放后,置為nullptr是個不錯的主意}ps = rhs.ps; // do the work of the move constructori = rhs.i;use = rhs.use; }return *this; } inline HasPtr& HasPtr::operator=(const HasPtr &rhs) {++*rhs.use; // increment the use count of the right-hand operandif (--*use == 0) { // then decrement this object's counterdelete ps;ps = nullptr; // if no other users delete use; use = nullptr; // free this object's allocated members}ps = rhs.ps; // copy data from rhs into this objecti = rhs.i;use = rhs.use; return *this; // return this object }#endif0和nullptr是一回事。作用一樣。
hasptr類值版本:
#ifndef HASPTR_H #define HASPTR_H#include <string> #include <iostream> #include <new> using namespace std; /** 注意:當(dāng)你釋放了某個對象的空間后,余下的部分可以交給編譯器去做。但是不能讓編譯器再使用* 這個對象的被釋放的值,否則會引發(fā)未定義的行為。比如:Segment fault,core dumped.* 比如: 執(zhí)行h1 = h2; 之后,h1的資源就被拷貝賦值運算符釋放掉了,此時h1(占用內(nèi)存空間的值)不能再使用 * 了,最后由編譯器釋放就可以了.*/ class HasPtr{public:HasPtr(const string &s = string());HasPtr(const HasPtr &);HasPtr& operator=(const HasPtr &);HasPtr(HasPtr &&)noexcept;HasPtr& operator=(HasPtr &&)noexcept;~HasPtr();private:string * ps;int i; }; inline HasPtr::HasPtr(const std::string &s): ps(new std::string(s)), i(0) {}// copy constructor copies all three data members // and increments the counter inline HasPtr::HasPtr(const HasPtr &p): ps(new string(*p.ps)), i(p.i) {}inline HasPtr::HasPtr(HasPtr &&p)noexcept: ps(p.ps), i(p.i){ p.ps = 0;} inline HasPtr::~HasPtr() { //you must charge whether the object is released before //f.g.it can operate delete use and delete ps,so it is released before,it can't released again at allif(!ps)delete ps; }inline HasPtr & HasPtr::operator=(HasPtr &&rhs)noexcept {//這里先判斷是不是自賦值情況,然后才能做賦值運算,如果先做賦值運算,那么釋放的對象就不對了if(this != &rhs){delete ps;ps = rhs.ps;i = rhs.i;rhs.ps = nullptr; } return *this; } inline HasPtr& HasPtr::operator=(const HasPtr &rhs) {string *new_ps = new string(*rhs.ps);delete ps;ps = nullptr; //這個很重要,提現(xiàn)代碼的嚴(yán)謹(jǐn)性,一會析構(gòu)函數(shù)需要檢測,檢測到nullptr,不用再釋放了,否則引發(fā)未定義行為ps = new_ps;i = rhs.i;return *this; // return this object }#endif13.6.3節(jié)練習(xí)
13.55
void StrBlob::push_back(string &&rhs) { data->push_back(std::move(rhs));//這里必須加std::move(),因為rhs是一個右值,但是是一個變量, //rhs本質(zhì)上是一個左值。所以要加std::move() }13.56
會導(dǎo)致無限循環(huán)(每次調(diào)用自己)。
13.57
Foo Foo::sorted()const &{return Foo(*this).sorted();} //Foo(*this)是構(gòu)造的一個臨時Foo對象,所以成功調(diào)用右值版本的成員函數(shù) #include <string> #include <iostream> #include <new> using namespace std; class Foo {private:int a = 0;public:Foo() = default;Foo(int b):a(b){}void sort()&&{cout << "call sort() &&" <<endl;}void sort()const &{Foo ret(*this);cout << "call sort()const&" << endl;return Foo(*this).sort(); }};int main() { Foo o1,o2,o3; o1.sort(); (Foo(o1)).sort();return 0; } call sort()const& //o1.sort()調(diào)用左值版本,但是左值版本會調(diào)用右值版本所以有2個輸出 call sort() && //也是左值版本的輸出 call sort() && //(Foo(o1)).sort()因為這個是調(diào)用右值版本,所以有一個輸出以下是自己先前寫的一些代碼,絕大部分都有問題。但是畢竟是自己親力所為,所以就不刪除了。
13.2節(jié)練習(xí)
練習(xí)13.22
#include <string> #include <iostream> #include <memory> using namespace std; class HasPtr{public: //這個構(gòu)造函數(shù)指定默認(rèn)初始值,s為 string()HasPtr(const string &s = string()):ps(new string(s)),i(0){cout << "call 默認(rèn)構(gòu)造函數(shù)" << endl;}HasPtr(const HasPtr&s):ps(new string(*s.ps)),i(s.i){cout << "call 拷貝構(gòu)造函數(shù)" << endl;}HasPtr&operator=(const HasPtr& s){i = s.i;//先拷貝底層string//這里有錯誤,ps原來指向的內(nèi)存沒有釋放掉,內(nèi)存泄漏.ps = new string(*s.ps);cout << "call 拷貝賦值運算符"<< endl; return *this;}~HasPtr(){delete ps;cout << "call 析構(gòu)函數(shù)" <<endl;}string&operator*(){return *ps;}HasPtr&operator=(const string &s){*ps = s;return *this;}private:int i;string *ps; };int main() { HasPtr h1("good2"); HasPtr h2("good1"); h2 = h1; h1 = h1; h2 = "hello,world"; cout << *h1 << endl; cout << *h2 << endl;return 0; }乍一看,好像好像上面的程序沒有問題,其實已經(jīng)發(fā)生嚴(yán)重錯誤——內(nèi)存泄露。
正確的拷貝賦值運算符定義如下:
HasPtr&operator=(const HasPtr& s){i = s.i;//先拷貝底層stringstring *new_ps = new string(*s.ps);delete ps;ps = new_ps;cout << "call 拷貝賦值運算符"<< endl; return *this;}練習(xí)13.27答案一:
#include <string> #include <iostream> #include <memory> using namespace std; class HasPtr{public:HasPtr(const string &s=string()):ps(new string(s)),i(0),use(new size_t(1)){use ++;}//下面++*use,指的是遞增use指向的引用計數(shù)HasPtr(const HasPtr &p):ps(p.ps),i(p.i),use(p.use){++ *use;}~HasPtr(){if(--*use == 0){delete ps;delete use;}}HasPtr& operator=(const HasPtr &p){++ *p.use;-- *use;if(*use == 0){delete ps;delete use;}ps = p.ps;i = p.i;use = p.use;return *this;}private:string *ps;int i;size_t * use; };int main() {return 0; }注意:切記不要出現(xiàn)++ use;出現(xiàn)這句,那么會把指針use指向別的單元。也就是執(zhí)行這句代碼后,use已經(jīng)不是指向原來use的內(nèi)存空間了。
13.27答案2:
#include <string> #include <iostream> #include <memory> using namespace std; class HasPtr{public:HasPtr(const string &s=string()):ps(new string(s)),i(0),use(new size_t(1)){cout <<"initialize:"<< "*use" << *use << endl;}HasPtr(const HasPtr &p):ps(p.ps),i(p.i),use(p.use){cout << "before add:"<<*use << endl;++ *use;cout << "copy constructor:" << *use << endl;}~HasPtr(){if(--*use == 0){delete ps;delete use;}}HasPtr& operator=(const HasPtr &p){++ *p.use;-- *use;if(*use == 0){delete ps;delete use;}ps = p.ps;i = p.i;use = p.use;return *this;}HasPtr& operator=(const string &p){//只是把一個新的string對象賦值給本HasPtr,其余什么都不變,注意這個思路的,這個string不能也算一個對象,所以不占用引用計數(shù)。*ps = p;return *this; }string& operator*(){return *ps; }size_t use_count()const{return *use;}private:string *ps;int i;size_t * use; };int main() { HasPtr p1("good"); HasPtr p2 = p1; cout << p1.use_count() << endl; cout << p2.use_count() << endl; cout << *p1 << endl; cout << *p2 << endl;return 0; }1.這段代碼增加了醫(yī)院運算符*,用來獲取HasPtr的string對象,結(jié)果是string&,所以結(jié)果是一個左值。
可以利用這句代碼改變HasPtr對象內(nèi)的string的值。
2.增加了string賦值,string賦值,僅僅改變一個HasPtr內(nèi)的string的值,其余內(nèi)容一切不改變。
總結(jié)
以上是生活随笔為你收集整理的c++ primer 5th第13章拷贝控制知识点和自编习题答案的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 引用做类中成员变量
- 下一篇: Visual Studio 2017 、