网站建设资讯

NEWS

网站建设资讯

C++11智能指针之weak-创新互联

含义

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

站在用户的角度思考问题,与客户深入沟通,找到济宁网站设计与济宁网站推广的解决方案,凭借多年的经验,让设计与互联网技术结合,创造个性化、用户体验好的作品,建站类型包括:网站设计制作、网站设计、企业官网、英文网站、手机端网站、网站推广、域名注册、虚拟空间、企业邮箱。业务覆盖济宁地区。函数lock

std::weak_ptr::lockshared_ptrlock() 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#includeusing namespace std;

int main()
{shared_ptrsp1(new int(11));
    shared_ptrsp2 = sp1;

    weak_ptrwp=sp1;
    cout<< "num="<< sp1.use_count()<< endl;
    cout<< "num="<< sp2.use_count()<< endl;
    cout<< "num="<< wp.use_count()<< endl;

    shared_ptrsp3 = 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_ptrtmp = wp.lock();
    if(tmp == nullptr)
    {cout<< "heap is null"<< endl;
    }

    return 0;
}

输出结果:
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#includeusing namespace std;

int main () {std::shared_ptrsp1,sp2;
    std::weak_ptrwp;
                                                               // sharing group:
                                                               // --------------
    sp1 = std::make_shared(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;
}

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

你是否还在寻找稳定的海外服务器提供商?创新互联www.cdcxhl.cn海外机房具备T级流量清洗系统配攻击溯源,准确流量调度确保服务器高可用性,企业级服务器适合批量采购,新人活动首月15元起,快前往官网查看详情吧


新闻名称:C++11智能指针之weak-创新互联
本文来源:http://njwzjz.com/article/ghgsp.html