Weak shared pointer
比較特殊的函數有l(wèi)ock和expired
std::weak_ptr::lockshared_ptr
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).
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
#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
你是否還在尋找穩(wěn)定的海外服務器提供商?創(chuàng)新互聯www.cdcxhl.cn海外機房具備T級流量清洗系統(tǒng)配攻擊溯源,準確流量調度確保服務器高可用性,企業(yè)級服務器適合批量采購,新人活動首月15元起,快前往官網查看詳情吧
當前標題:C++11智能指針之weak-創(chuàng)新互聯
網頁地址:http://chinadenli.net/article38/ghgsp.html
成都網站建設公司_創(chuàng)新互聯,為您提供服務器托管、定制網站、品牌網站制作、網站排名、企業(yè)網站制作、全網營銷推廣
聲明:本網站發(fā)布的內容(圖片、視頻和文字)以用戶投稿、用戶轉載內容為主,如果涉及侵權請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網站立場,如需處理請聯系客服。電話:028-86922220;郵箱:631063699@qq.com。內容未經允許不得轉載,或轉載時需注明來源: 創(chuàng)新互聯