Smart pointer: weak_ptr (weak reference smart pointer)

The weak reference smart pointer std::weak_ptr can be regarded as the assistant of shared_ptr, it does not manage the internal pointer of shared_ptr. std::weak_prt does not overload operators * and ->, because it does not share pointers and cannot operate resources, so its construction will not increase the reference count, and its destruction will not decrease the reference count. Its main function is as a A bystander monitors the existence of a resource managed in a shared_ptr.

initialization:

#include<iostream>
#include<memory>
using namespace std;

int main()
{
    shared_ptr<int> sp(new int);

    //weak_ptr<int> wp1; 构造一个空weak_ptr对象
    weak_ptrr<int> wp1;

    //weak_ptr<int> wp2(wp1);    通过一个空weak_ptr对象构造了另一个空weak_ptr对象
    weak_ptr<int> wp2(wp1);

    //weak_ptr<int> wp3(sp);通过一个shared_ptr对象构造了一个可用的weak_ptr实例对象

    weak_ptr<int> wp3(sp);

    //wp4 = sp;    通过一个shared_ptr对象构造了一个可用的weak_ptr实例对象(这是一个隐式类型转换)
    weak_ptr<int> wp4;
    wp4 = sp;

    //wp5 = wp3; 通过一个weak_ptr对象构造了一个可用的weak_ptr实例对象
    weak_ptr<int> wp5;
    wp5 = wp3;

    return 0;
}

use_count()

The reference count of the currently observed resource can be obtained by calling the use_count() method provided by the std::weak_ptr class

shared_ptr<int> sp(new int);

weak_ptr<int> wp1;
weak_ptr<int> wp2(wp1);
weak_ptr<int> wp3(sp);
weak_ptr<int> wp4;
wp4 = sp;
weak_ptr<int> wp5;
wp5 = wp3;

cout<<"use_count:"<<endl;
cout<<"wp1:"<<wp1.use_count()<<endl;
cout<<"wp2:"<<wp2.use_count()<<endl;
cout<<"wp3:"<<wp3.use_count()<<endl;
cout<<"wp4:"<<wp4.use_count()<<enld;


cout<<"wp5:"<<wp5.use_count<<endl;
return 0;

use_count:
wp1:0
wp2:0
wp3:1
wp4:1
wp5:1

From the printed results, we can know that although the weak reference smart pointers wp3, wp4, and wp5 monitor the same resource, their reference counts have not changed, which further proves that weak_ptr only monitors resources, not manages resources.

 expired()

Determine whether the observed resource has been released by calling the expired() method provided by the std::weak_ptr class

int main()
{
    shared_ptr<int> shared(new int(10));
    weak_ptr<int> weak(shared);
    cout<<"1.weak"<<(weak.expired()?"is":"is not")<<"expired"<<endl;

    shared.reset();
    cout<<"2.weak"<<(weak.expired()?"is":"is nor")<<"expired"<<endl;

    return 0;
}

//1.weak is not expired
//2.weak is expired

 weak_ptr monitors the resources managed by shared_ptr. When the shared smart pointer calls shared.reset(); the managed resources are released, so the result of the weak.expired() function returns true, indicating that the monitored resources are no longer available.

 lock()

Obtain the shared_ptr object that manages the monitored resource by calling the lock() method provided by the std::weak_ptr class

int main()
{
    shared_ptr<int> sp1,sp2;
    weak_ptr<int> wp;
    
    sp1 = std::make_shared<int>(520);
    wp = sp1;
    sp2 = wp.lock();
    cout<<"use_count:"<<wp.use_count()<<endl;

    sp1.reset();
    cout<<"use_count:"<<wp.use_count()<<endl;

    sp1 = wp.lock();
    cout<<"use_count:"<<wp.use_count()<<endl;

    cout<<"*sp1:"<< *sp1<<endl;
    coutMM"*sp2:"<< *sp2<<endl;

    reutrn 0;
}


use_count:2
use_count:1
use_count:2
*sp1:520
*sp2:520

sp2 = wp.lock(); Obtain a shared smart pointer object used to manage the resource monitored by the weak_ptr object by calling the lock() method, use this object to initialize sp2, and the reference count of the monitored resource is 2, sp1. reset(); The shared smart pointer sp1 is reset, the reference count of the resource monitored by the weak_ptr object is decremented by 1, sp1=wp.lock(); sp1 is re-initialized, and it still manages the resource monitored by the weak_ptr object, so the reference The count is increased by 1, and the shared smart pointer objects sp1 and sp2 manage the same block of memory, so the results in the final printed memory are the same, both are 520.

Guess you like

Origin blog.csdn.net/four_two_six_/article/details/130887088