成人午夜视频全免费观看高清-秋霞福利视频一区二区三区-国产精品久久久久电影小说-亚洲不卡区三一区三区一区

C++11智能指針之weak-創(chuàng)新互聯(lián)

含義

Weak shared pointer
比較特殊的函數(shù)有l(wèi)ock和expired

站在用戶的角度思考問(wèn)題,與客戶深入溝通,找到濟(jì)寧網(wǎng)站設(shè)計(jì)與濟(jì)寧網(wǎng)站推廣的解決方案,憑借多年的經(jīng)驗(yàn),讓設(shè)計(jì)與互聯(lián)網(wǎng)技術(shù)結(jié)合,創(chuàng)造個(gè)性化、用戶體驗(yàn)好的作品,建站類型包括:網(wǎng)站設(shè)計(jì)制作、網(wǎng)站設(shè)計(jì)、企業(yè)官網(wǎng)、英文網(wǎng)站、手機(jī)端網(wǎng)站、網(wǎng)站推廣、域名注冊(cè)、虛擬空間、企業(yè)郵箱。業(yè)務(wù)覆蓋濟(jì)寧地區(qū)。函數(shù)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).

函數(shù)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;
    //當(dāng)堆空間釋放后
    shared_ptrtmp = wp.lock();
    if(tmp == nullptr)
    {cout<< "heap is null"<< endl;
    }

    return 0;
}

輸出結(jié)果:
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;
}

結(jié)果如下:
num= 1
num=0
num=1
num2= 0
num2=1
num2=1
*sp1: 20
*sp2: 20
num3= 2
num3=2
num3=2

你是否還在尋找穩(wěn)定的海外服務(wù)器提供商?創(chuàng)新互聯(lián)www.cdcxhl.cn海外機(jī)房具備T級(jí)流量清洗系統(tǒng)配攻擊溯源,準(zhǔn)確流量調(diào)度確保服務(wù)器高可用性,企業(yè)級(jí)服務(wù)器適合批量采購(gòu),新人活動(dòng)首月15元起,快前往官網(wǎng)查看詳情吧

當(dāng)前標(biāo)題:C++11智能指針之weak-創(chuàng)新互聯(lián)
網(wǎng)頁(yè)地址:http://jinyejixie.com/article38/ghgsp.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供服務(wù)器托管、定制網(wǎng)站、品牌網(wǎng)站制作、網(wǎng)站排名、企業(yè)網(wǎng)站制作全網(wǎng)營(yíng)銷推廣

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來(lái)源: 創(chuàng)新互聯(lián)

微信小程序開(kāi)發(fā)
南漳县| 斗六市| 定安县| 永仁县| 沂水县| 湾仔区| 昌邑市| 琼海市| 陇南市| 杨浦区| 上饶县| 修水县| 桐庐县| 乌兰浩特市| 高安市| 衡东县| 鸡西市| 古交市| 深水埗区| 大方县| 道孚县| 灵璧县| 左云县| 耒阳市| 三河市| 吉安市| 敦煌市| 广昌县| 渭南市| 政和县| 惠东县| 长武县| 涞源县| 瑞安市| 上犹县| 鄄城县| 加查县| 定西市| 额敏县| 满城县| 隆化县|