智能指针——weak_ptr
1. 開篇
正式介紹weak_ptr之前,先來復習一下shared_ptr 的一些知識。
我們知道shared_ptr是采用引用計數(shù)的智能指針,多個shared_ptr實例可以指向同一個動態(tài)對象,并維護了一個共享的引用計數(shù)器。
對于引用計數(shù)法實現(xiàn)的計數(shù),總是避免不了循環(huán)引用(或環(huán)形引用)的問題,shared_ptr也不例外。
示例
class Child;class Parent { public:shared_ptr<Child> child;~Parent() { cout << "Bye Parent" << endl; }void hi() const { cout << "He1lo" << endl; } };class Child { public:shared_ptr<Parent> parent;~Child() { cout << "Bye Child" << endl; } };int main() {shared_ptr<Parent> parent = make_shared<Parent>();shared_ptr<Child> child = make_shared<Child>();parent->child = child;child->parent = parent;child- >parent->hi();return 0; }運行結果
上面代碼的運行結果,只打印出”Hello”,而并沒有打印出"Bye Parent"或"Bye Child",說明Parent和Child的析構函數(shù)并沒有調(diào)用到。這是因為Parent和Child對象內(nèi)部,具有各自指向對方的shared_ptr,加上parent和child這兩個shared_ptr,說明每個對象的引用計數(shù)都是2。當程序退出時,即使parent和child被銷毀,也僅僅是導致引用計數(shù)變?yōu)榱?,因此并未銷毀arent和Child對象。
為了解決類似這樣的問題,C++11引入了weak_ptr,來打破這種循環(huán)引用。
2. weak_ptr的概念
weak_ptr是為了配合shared_ptr而引入的一種智能指針,它指向一個由 shared_ptr管理的對象而不影響所指對象的生命周期,也就是將一個weak_ptr綁定到一個shared_ptr不會改變shared_ptr的引用計數(shù)。
不論是否有weak_ptr指向,一旦最后一個指向對象的shared_ptr被銷毀,對象就會被釋放。
從這個角度看,weak_ptr更像是shared_ptr的一個助手而不是智能指針。
3. weak_ptr的使用
3.1 如何創(chuàng)建weak_ptr實例
當我們創(chuàng)建一個weak_ptr時,需要用一個shared_ptr實例來初始化 weak_ptr,由于是弱共享,weak_ptr的創(chuàng)建并不會影響shared_ptr的引用計數(shù)值。
int main() {shared_ptr<int> sp(new int(5));cout <<"創(chuàng)建前sp的引用計數(shù):" << sp.use_count() << endl; //use_count = 1weak_ptr<int> wp(sp);cout <<"創(chuàng)建后sp的引用計數(shù): " << sp.use_count() << endl; //use_count = 1return 0; }3.2 如何判斷weak_ptr指向對象是否存在
既然weak_ptr并不改變其所共享的shared_ptr實例的引用計數(shù),那就可能存在weak_ptr指向的對象被釋放掉這種情況。
這時,我們就不能使用weak_ptr直接訪問對象。那么我們?nèi)绾闻袛鄔eak_ptr指向對象是否存在呢?
C++中提供了lock函數(shù)來實現(xiàn)該功能。
如果對象存在,lock()函數(shù)返回一個指向共享對象的shared_ptr,否則返回一個空shared_ptr。
3.3 如何使用weak_ptr
weak_ptr并沒有重載operator->和operator*操作符,因此不可直接通過weak_ptr使用對象,典型的用法是調(diào)用其lock函數(shù)來獲得shared_ptr示例,進而訪問原始對象。
使用weak_ptr來改造最前面的代碼,打破循環(huán)引用問題。
class Child;class Parent { public:weak_ptr<Child> child; //~parent() { cout << "Bye Parent" <<endl; }void hi() const { cout<< "Hello" << endl; } };class Child { public:weak_ptr<Parent> parent; //~Child() { cout << "Bye Child" << endl; } };int main() {shared_ptr<Parent> parent = make_shared<Parent>();shared_ptr<Child> child = make_shared<Child>();parent->child = child;child->parent = parent;//child->parent->hi(); //??if(child->expired()) { child->parent.lock()->hi(); }return 0; }總結
以上是生活随笔為你收集整理的智能指针——weak_ptr的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 智能指针——unique_ptr
- 下一篇: 二叉树的基础知识