您好,登錄后才能下訂單哦!
1、共享性智能指針(shared_ptr)
引用計(jì)數(shù)型指針
shared_ptr是一個(gè)最像指針的“智能指針”,是boost.smart_ptr庫(kù)中最有價(jià)值,最重要,也是最有用的。
shared_ptr實(shí)現(xiàn)的是引用技術(shù)型的智能指針,可以被拷貝和賦值,在任意地方共享它,當(dāng)沒有代碼使用(此時(shí)引用計(jì)數(shù)為0)它才刪除被動(dòng)態(tài)分配的對(duì)象。shared_ptr也可以被安全的放到標(biāo)準(zhǔn)容器中;
2、怎么使用shared_ptr
舉一個(gè)操作的例子:
#include<iostream> #include<boost/smart_ptr.hpp> using namespace std; using namespace boost; int main(void){ int *p = new int(10); shared_ptr<int> ps(p); // cout<<*ps<<endl; cout<<ps.unique()<<endl; //判斷對(duì)空間是否唯一, cout<<ps.use_count()<<endl; shared_ptr<int> ps1 = ps; cout<<ps.unique()<<endl; //此時(shí)有兩個(gè)對(duì)空間有共享,所以不唯一,是0 cout<<ps.use_count()<<endl; shared_ptr<int> ps2; ps2 = ps1; cout<<ps.use_count()<<endl; }
關(guān)鍵在shared_ptr中存在共享引用計(jì)數(shù)。
3、框架的搭建
閱讀源代碼:
shared_ptr 中的私有數(shù)據(jù)成員:
private: T *px; shared_count pn; //對(duì)象成員,肯定先調(diào)這個(gè)對(duì)象的構(gòu)造函數(shù);
之前的引用計(jì)數(shù)通過(guò)一個(gè)指針,現(xiàn)在的引用計(jì)數(shù)通過(guò)一個(gè)對(duì)象,pn
構(gòu)造函數(shù)的調(diào)用順序:先虛基類,父類,對(duì)象成員,最后構(gòu)造自己;
此時(shí)的模型如下:
其后調(diào)用對(duì)象成員的構(gòu)造函數(shù):
shared_counted中的私有數(shù)據(jù)成員:
private: sp_counted_base *pi; //有一個(gè)指向引用計(jì)數(shù)器父類的指針;
此時(shí)就得先寫:sp_counted_base類了;
sp_counted_base類中的私有數(shù)據(jù)成員:
private: long use_count_;
然后看到在shared_counted的構(gòu)造函數(shù):
public: template<class T> //此時(shí)類型不定,寫模板函數(shù) shared_count(T *p) : pi(new sp_counted_impl_xx(p)){ //特別重要,這個(gè)構(gòu)造函數(shù)
此時(shí)就得寫sp_counted_impl_xx類了:這是繼承sp_counted_base類
其內(nèi)部數(shù)據(jù)時(shí)成員:
private: T *px_;
此時(shí)整體的建構(gòu)體系就已經(jīng)形成:
我認(rèn)為是這樣的:
(1)、先實(shí)現(xiàn)了shared_ptr類,因?yàn)橛袑?duì)象成員,其后調(diào)用構(gòu)造函數(shù),
(2)、實(shí)現(xiàn)了shared_count; 其數(shù)據(jù)成員有sp_counted_base,
(3)、因?yàn)榫幾g器的順序,先類名,在數(shù)據(jù)成員,最后函數(shù),所以此時(shí)先實(shí)現(xiàn)sp_counted_base;
(4)、因?yàn)閟hared_counted中的構(gòu)造函數(shù)要在堆上開辟sp_counted_impl_xx空間,最后實(shí)現(xiàn)是sp_counted_impl_xx,它有繼承sp_counted_base,所以構(gòu)造函數(shù)的調(diào)用順序就很清楚了。
構(gòu)造函數(shù)的調(diào)用順序:sp_counted_base、sp_counted_impl_xx、shared_count、shared_ptr
此時(shí)的具體實(shí)現(xiàn)代碼如下:
#ifndef _CONFIG_H_ #define _CONFIG_H_ #include<iostream> using namespace std; #endif //////////////////////////////////////////////////////////////////////////// #ifndef _SHARED_PTR_H_ #define _SHARED_PTR_H_ #include"shared_count.h" template<class T> class shared_ptr{ public: shared_ptr(T *p = 0) : px(p), pn(p){ cout<<"Create shared_ptr object!"<<endl; } ~shared_ptr(){ cout<<"Free shared_ptr object"<<endl; } private: T *px; shared_count pn; }; #endif /////////////////////////////////////////////////////////////////////////////// #ifndef _SHARED_COUNT_H_ #define _SHARED_COUNT_H_ #include"config.h" #include"sp_counted_base.h" #include"sp_counted_impl_xx.h" class shared_count{ public: template<class T> //此時(shí)類型不定,寫模板函數(shù) shared_count(T *p) : pi(new sp_counted_impl_xx<T>(p)){ cout<<"Create shared_cout object!"<<endl; } ~shared_count(){ cout<<"Free shared_count object"<<endl; } private: sp_counted_base *pi; }; #endif /////////////////////////////////////////////////////////////////////////////// #ifndef SP_COUNTED_BASE_H_ #define SP_COUNTED_BASE_H_ #include"config.h" class sp_counted_base{ public: sp_counted_base() : use_count_(1){ cout<<"Create sp_counted_base object"<<endl; } ~sp_counted_base(){ cout<<"Free sp_counted_base object"<<endl; } private: long use_count_; }; #endif ////////////////////////////////////////////////////////////////////////////////////// #ifndef SP_COUNTED_IMPL_XX_H_ #define SP_COUNTED_IMPL_XX_H_ #include"sp_counted_base.h" template<class T> class sp_counted_impl_xx : public sp_counted_base{ public: sp_counted_impl_xx(T *p) : px_(p){ cout<<"Create sp_counted_impl_xx object"<<endl; } ~sp_counted_impl_xx(){ cout<<"Free sp_counted_impl_xx object"<<endl; } private: T *px_; }; #endif ////////////////////////////////////////////////////////////////////////////////////////////// #include<iostream> #include"shared_ptr.h" using namespace std; int main(void){ int *p = new int(10); shared_ptr<int> ps(p); }
以下是運(yùn)行結(jié)果:
以上就是只搭好了大致的框架,并沒有考慮內(nèi)存泄漏,析構(gòu)的具體寫法和其它函數(shù)的實(shí)現(xiàn);
那么整個(gè)模型如下:
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。