• C++11智能指针之weak_ptr


    含义

    Weak shared pointer
    比较特殊的函数有lock和expired

    函数lock

    std::weak_ptr::lockshared_ptr lock() const noexcept;
    Lock and restore weak_ptr.
    Returns a shared_ptr with the information preserved by the weak_ptr object if it is not expired.
    If the weak_ptr object has expired (including if it is empty), the function returns an empty shared_ptr (as if default-constructed).
    Because shared_ptr objects count as an owner, this function locks the owned pointer, preventing it from being released (for at least as long as the returned object does not releases it).

    函数expired

    std::weak_ptr::expiredbool expired() const noexcept;
    Check if expired
    Returns whether the weak_ptr object is either empty or there are no more shared_ptr in the owner group it belongs to.

    Expired pointers act as empty weak_ptr objects when locked, and thus can no longer be used to restore an owning shared_ptr.

    This function shall return the same as (use_count()==0), although it may do so in a more efficient way.

    举例

    例子1

    #include 
    #include 
    using namespace std;
    
    int main()
    {
        shared_ptr<int> sp1(new int(11));
        shared_ptr<int> sp2 = sp1;
    
        weak_ptr<int> wp=sp1;
        cout << "num=" << sp1.use_count() << endl;
        cout << "num=" << sp2.use_count() << endl;
        cout << "num=" << wp.use_count() << endl;
    
        shared_ptr<int> sp3 = wp.lock();
        cout << "num2=" << sp1.use_count() << endl;
        cout << "num2=" << sp2.use_count() << endl;
        cout << "num2=" << sp3.use_count() << endl;
        cout << "num2=" << wp.use_count() << endl;
    
        sp1.reset();
        sp2.reset();
        sp3.reset();
        cout << "num3=" << sp1.use_count() << endl;
        cout << "num3=" << sp2.use_count() << endl;
        cout << "num3=" << sp3.use_count() << endl;
        cout << "num3=" << wp.use_count() << endl;
        //当堆空间释放后
        shared_ptr<int> tmp = wp.lock();
        if(tmp == nullptr)
        {
            cout << "heap is null" << endl;
        }
    
        return 0;
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36

    输出结果:
    num=2
    num=2
    num=2
    num2=3
    num2=3
    num2=3
    num2=3
    num3=0
    num3=0
    num3=0
    num3=0
    heap is null

    例子2

    #include 
    #include 
    using namespace std;
    
    int main () {
        std::shared_ptr<int> sp1,sp2;
        std::weak_ptr<int> wp;
                                                                   // sharing group:
                                                                   // --------------
        sp1 = std::make_shared<int> (20);    // sp1
        wp = sp1;                                           // sp1, wp
    
        std::cout << "num= " << sp1.use_count() << '\n';
        std::cout << "num=" << sp2.use_count() << '\n';
        std::cout << "num=" << wp.use_count() << '\n';
    
        sp2 = wp.lock();                                // sp1, wp, sp2
        sp1.reset();                                        //      wp, sp2
    
        std::cout << "num2= " << sp1.use_count() << '\n';
        std::cout << "num2=" << sp2.use_count() << '\n';
        std::cout << "num2=" << wp.use_count() << '\n';
    
        sp1 = wp.lock();                                // sp1, wp, sp2
    
        std::cout << "*sp1: " << *sp1 << '\n';
        std::cout << "*sp2: " << *sp2 << '\n';
        std::cout << "num3= " << sp1.use_count() << '\n';
        std::cout << "num3=" << sp2.use_count() << '\n';
        std::cout << "num3=" << wp.use_count() << '\n';
    
      return 0;
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33

    结果如下:
    num= 1
    num=0
    num=1
    num2= 0
    num2=1
    num2=1
    *sp1: 20
    *sp2: 20
    num3= 2
    num3=2
    num3=2

  • 相关阅读:
    Spring面试题学习: 单例Bean是单例模式吗?
    零日漏洞预防
    Java-类和对象
    好奇喵 | Tor浏览器——如何拥有一颗洋葱并使用
    两数之和-(哈希)
    完整数字华容道03:首页创建
    【密码学】RSA的攻与防_4.0
    快收藏!最全GO语言实现设计模式【下】
    Axi接口的DDR3:参数,时序,握手机制
    【2023研电赛】安谋科技企业命题二等奖:基于R329的AI交互早教机器人
  • 原文地址:https://blog.csdn.net/xiongpursuit88/article/details/128157578