- Learning C++ Functional Programming
- Wisnu Anggoro
- 539字
- 2021-07-02 20:51:39
Tracking the objects using a weak_ptr pointer
We have discussed the shared_ptr in the preceding section. The pointer is actually a little bit fat pointer. It logically points to two objects, the object being managed and the pointing counter using the use_count() method. Every shared_ptr basically has a strong reference count that prevents the object from being deleted and a weak reference count that does not prevent the object being deleted if the shared_ptr object's use count reaches 0, although we don't even use the weak reference count. For this reason, we can use only one reference count so we can use the weak_ptr pointer. The weak_ptr pointer refers to an object that is managed by shared_ptr. The advantage of weak_ptr is that it can be used to refer to an object, but we can only access it if the object still exists and without preventing the object from being deleted by some other reference holder if the strong reference count reaches zero. It is useful when we deal with data structures. Let's take a look at the following block of code to analyze the use of weak_ptr:
/* weak_ptr_1.cpp */
#include <memory>
#include <iostream>
using namespace std;
auto main() -> int
{
cout << "[weak_ptr_1.cpp]" << endl;
auto sp = make_shared<int>(1234);
auto wp = weak_ptr<int>{ sp };
if(wp.expired())
cout << "wp is expired" << endl;
else
cout << "wp is not expired" << endl;
cout << "wp pointing counter = " << wp.use_count() << endl;
if(auto locked = wp.lock())
cout << "wp is locked. Value = " << *locked << endl;
else
{
cout << "wp is unlocked" << endl;
wp.reset();
}
cout << endl;
sp = nullptr;
if(wp.expired())
cout << "wp is expired" << endl;
else
cout << "wp is not expired" << endl;
cout << "wp pointing counter = " << wp.use_count() << endl;
if(auto locked = wp.lock())
cout << "wp is locked. Value = " << *locked << endl;
else
{
cout << "wp is unlocked" << endl;
wp.reset();
}
cout << endl;
return 0;
}
Before we analyze the preceding code, let's take a look at the following screenshot from the output console if we run the code:

At first, we instantiate shared_ptr and, as we discussed previously, the weak_ptr points to the object managed by shared_ptr. We then assign wp to the shared_ptr variable, sp. After we have a weak_ptr pointer, we then check its behavior. By calling the expired() method, we can figure out whether the referenced object was already deleted. And, since the wp variable is just constructed, it is not expired yet. The weak_ptr pointer also holds the value of the object counting by calling the use_count() method, as we used in shared_ptr. We then invoke the locked() method to create a shared_ptr that manages the referenced object and finds the value weak_ptr is pointing at. We now have a shared_ptr variable pointing to the address that holds the 1234 value.
We reset sp to nullptr afterward. Although we don't touch the weak_ptr pointer, it is also changed. As we can see from the console screenshot, now wp is expired since the object has been deleted. The counter also changes and becomes 0 since it points to nothing. Moreover, it is unlocked since the shared_ptr object has been deleted.
- 零基礎(chǔ)學(xué)Visual C++第3版
- 測試驅(qū)動開發(fā):入門、實戰(zhàn)與進(jìn)階
- Visual FoxPro 程序設(shè)計
- 青少年美育趣味課堂:XMind思維導(dǎo)圖制作
- Cocos2d-x學(xué)習(xí)筆記:完全掌握Lua API與游戲項目開發(fā) (未來書庫)
- 青少年信息學(xué)競賽
- 計算機(jī)應(yīng)用基礎(chǔ)教程(Windows 7+Office 2010)
- 小程序從0到1:微信全棧工程師一本通
- IoT Projects with Bluetooth Low Energy
- Scala編程實戰(zhàn)
- Python預(yù)測分析與機(jī)器學(xué)習(xí)
- Visual Basic程序設(shè)計實驗指導(dǎo)及考試指南
- Learning Cocos2d-JS Game Development
- Pandas 1.x Cookbook
- 編程風(fēng)格:程序設(shè)計與系統(tǒng)構(gòu)建的藝術(shù)(原書第2版)