詳解C++?中?shared_ptr?weak_ptr
shared_ptr
shared_ptr 是一個(gè)標(biāo)準(zhǔn)的共享所有權(quán)的智能指針,允許多個(gè)指針指向同一個(gè)對(duì)象,定義在 memory 文件中,命名空間為 std。shared_ptr最初實(shí)現(xiàn)于Boost庫(kù)中,后由 C++11 引入到 C++ STL。shared_ptr 利用引用計(jì)數(shù)的方式實(shí)現(xiàn)了對(duì)所管理的對(duì)象的所有權(quán)的分享,即允許多個(gè) shared_ptr 共同管理同一個(gè)對(duì)象。
std::shared_ptr<int> sp1 = new int(); // shared count = 1, weak count = 0 std::shared_ptr<int> sp2(sp1); // shared count = 2, weak count = 0 std::shared_ptr<int> sp3 = sp2; // shared count = 3, weak count = 0 std::weak_ptr<int> wp1(sp1); // shared count = 3, weak count = 1 std::weak_ptr<int> wp2(wp1); // shared count = 3, weak count = 2 std::weak_ptr<int> wp3 = wp2; // shared count = 3, weak count = 3
shared_ptr weak_ptr 使用 reset 或者指向另一個(gè) managed object導(dǎo)致 shared count或weak count相應(yīng)的減一。
1.類繼承中使用shared_ptr
class Base {}; class Derived : public Base {}; ...... shared_ptr<Derived> dp1(new Derived); shared_ptr<Base> bp1 = dp1; shared_ptr<Base> bp2(dp1); shared_ptr<Base> bp3(new Derived);
2.casting shared_ptr
shared_ptr<Base> base_ptr (new Base); shared_ptr<Derived> derived_ptr; // if static_cast<Derived *>(base_ptr.get()) is valid, then the following is valid: derived_ptr = static_pointer_cast<Derived>(base_ptr);
3.make_shared
使用shared_ptr = new int(),會(huì)導(dǎo)致兩次內(nèi)存分配:int對(duì)象的內(nèi)存分配跟shared_ptr內(nèi)部的 manager object 一次內(nèi)存分配。make_shared 對(duì)此進(jìn)行了優(yōu)化,一次性分配 int + manager object 內(nèi)存空間大小。
make_shared 用法:
shared_ptr<Thing> p (make_shared<Thing>(42, "I'm a Thing!")); shared_ptr<Base> bp(make_shared<Derived1>()); // shared_ptr中的 template參數(shù)與make_shared中的tmeplate參數(shù)可以不一樣(繼承關(guān)系)
使用 weak_ptr
void do_it(weak_ptr<Thing> wp){ shared_ptr<Thing> sp = wp.lock(); // get shared_ptr from weak_ptr if(sp) sp->defrangulate(); // tell the Thing to do something else cout << "The Thing is gone!" << endl; }
也可以直接從weak_ptr構(gòu)建shared_ptr,這個(gè)時(shí)間如果weak_ptr過(guò)期(通過(guò) weak_ptr::expired() 可以查詢),則拋出異常:
void do_it(weak_ptr<Thing> wp){ shared_ptr<Thing> sp(wp); // construct shared_ptr from weak_ptr // exception thrown if wp is expired, so if here, sp is good to go sp->defrangulate(); // tell the Thing to do something }
到此這篇關(guān)于C++ 中 shared_ptr weak_ptr的文章就介紹到這了,更多相關(guān)C++ shared_ptr weak_ptr內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
C++拷貝構(gòu)造函數(shù)和賦值運(yùn)算符重載詳解
拷貝構(gòu)造函數(shù)是特殊的構(gòu)造函數(shù),是用一個(gè)已經(jīng)存在的對(duì)象,賦值拷貝給另一個(gè)新創(chuàng)建的已經(jīng)存在的對(duì)象,這篇文章主要介紹了C++拷貝構(gòu)造函數(shù)和賦值運(yùn)算符重載,需要的朋友可以參考下2024-05-05C語(yǔ)言中計(jì)算正弦的相關(guān)函數(shù)總結(jié)
這篇文章主要介紹了C語(yǔ)言中計(jì)算正弦的相關(guān)函數(shù)總結(jié),包括正弦和雙曲線正弦以及反正弦的函數(shù),需要的朋友可以參考下2015-08-08Cocos2d-x保存用戶游戲數(shù)據(jù)之XML文件是否存在問(wèn)題判斷方法
這篇文章主要介紹了Cocos2d-x保存用戶游戲數(shù)據(jù)之XML文件是否存在問(wèn)題判斷方法,請(qǐng)注意代碼中包含大量注釋,需要的朋友可以參考下2014-09-09C++普通函數(shù)指針與成員函數(shù)指針實(shí)例解析
這篇文章主要介紹了C++普通函數(shù)指針與成員函數(shù)指針,很重要的知識(shí)點(diǎn),需要的朋友可以參考下2014-08-08Linux下動(dòng)靜態(tài)庫(kù)的打包與使用指南(C/C++)
c++是面向?qū)ο蟮木幊陶Z(yǔ)言,比較方便實(shí)現(xiàn)某些第三方庫(kù),比如翻譯其他面向?qū)ο笳Z(yǔ)言的代碼,比c語(yǔ)言要方便的多,下面這篇文章主要給大家介紹了關(guān)于Linux下C/C++動(dòng)靜態(tài)庫(kù)的打包與使用的相關(guān)資料,需要的朋友可以參考下2023-02-02C語(yǔ)言數(shù)據(jù)結(jié)構(gòu)之線性表的鏈?zhǔn)酱鎯?chǔ)結(jié)構(gòu)
線性表是最基本、最簡(jiǎn)單、也是最常用的一種數(shù)據(jù)結(jié)構(gòu)。線性表(linear list)是數(shù)據(jù)結(jié)構(gòu)的一種,一個(gè)線性表是n個(gè)具有相同特性的數(shù)據(jù)元素的有限序列,這篇文章帶你學(xué)習(xí)下線性表的鏈?zhǔn)酱鎯?chǔ)結(jié)構(gòu)2021-11-11C++基于OpenCV實(shí)現(xiàn)手勢(shì)識(shí)別的源碼
這篇文章主要介紹了C++基于OpenCV手勢(shì)識(shí)別的實(shí)現(xiàn)源碼,這里用到背景減法模型知識(shí),具體實(shí)例代碼跟隨小編一起看看吧2021-09-09