国产亚洲精品福利在线无卡一,国产精久久一区二区三区,亚洲精品无码国模,精品久久久久久无码专区不卡

當(dāng)前位置: 首頁(yè) > news >正文

瀘州市住房與城鄉(xiāng)建設(shè)局網(wǎng)站google免費(fèi)入口

瀘州市住房與城鄉(xiāng)建設(shè)局網(wǎng)站,google免費(fèi)入口,wordpress禁止主題更新,ui設(shè)計(jì)需要學(xué)什么介紹 shared_ptr是一種智能指針(smart pointer),作用有如同指針,但會(huì)記錄有多少個(gè)shared_ptrs共同指向一個(gè)對(duì)象。這便是所謂的引用計(jì)數(shù)(reference counting),比如我們把只能指針賦值給另外一個(gè)對(duì)象,那么對(duì)象多了一個(gè)智能指針指向它,所以這個(gè)時(shí)候引用計(jì)數(shù)…

介紹

shared_ptr是一種智能指針(smart pointer),作用有如同指針,但會(huì)記錄有多少個(gè)shared_ptrs共同指向一個(gè)對(duì)象。這便是所謂的引用計(jì)數(shù)(reference counting),比如我們把只能指針賦值給另外一個(gè)對(duì)象,那么對(duì)象多了一個(gè)智能指針指向它,所以這個(gè)時(shí)候引用計(jì)數(shù)會(huì)增加一個(gè),我們可以用shared_ptr.use_count()函數(shù)查看這個(gè)智能指針的引用計(jì)數(shù)。

在這里插入圖片描述
下面放上c++參考手冊(cè)的介紹:
在這里插入圖片描述在這里插入圖片描述在這里插入圖片描述在這里插入圖片描述
具體對(duì)應(yīng)模塊參考鏈接:https://zh.cppreference.com/w/cpp/memory/shared_ptr

示例:

#include <iostream>
#include <memory>   //使用shared_ptr需要include它int main() {//通過(guò)make_shared創(chuàng)建shared_ptrstd::shared_ptr<int> p1 = std::make_shared<int>();*p1 = 78;std::cout << "p1 = " << *p1 << std::endl;//查看引用計(jì)數(shù)std::cout << "p1 Reference count = " << p1.use_count() << std::endl;//第二個(gè)shared_ptr也將在內(nèi)部指向相同的指針//這將會(huì)使引用計(jì)數(shù)變?yōu)?std::shared_ptr<int> p2(p1);//查看引用計(jì)數(shù)std::cout << "p2 Reference count = " << p2.use_count() << std::endl;std::cout << "p1 Reference count = " << p1.use_count() << std::endl;//比較智能指針if (p1 == p2) {std::cout << "p1 and p2 are pointing to same pointer\n";}std::cout << "Reset p1" << std::endl;//重置shared_ptr,在這種情況下,其內(nèi)部不會(huì)指向內(nèi)部的任何指針//因此其引用計(jì)數(shù)將會(huì)變?yōu)?p1.reset();std::cout << "p1 Reference Count = " << p1.use_count() << std::endl;//重置shared_ptr,在這種情況下,其內(nèi)部將會(huì)指向一個(gè)新的指針//因此其引用計(jì)數(shù)將會(huì)變?yōu)?p1.reset(new int(11));std::cout << "p1 Reference Count = " << p1.use_count() << std::endl;//分配nullptr將取消關(guān)聯(lián)指針并使其指向空值p1 = nullptr; std::cout << "p1 Reference Count = " << p1.use_count() << std::endl;if (!p1) {std::cout << "p1 is NULL" << std::endl;}return 0;
}

輸出:

p1 = 78
p1 Reference count = 1
p2 Reference count = 2
p1 Reference count = 2
p1 and p2 are pointing to same pointer
Reset p1 
p1 Reference Count = 0
p1  Reference Count = 1
p1  Reference Count = 0
p1 is NULL

下面討論下怎樣使用 std::shared_ptr自定義Deleter.
當(dāng)一個(gè)shared_ptr對(duì)象超出作用域時(shí),其析構(gòu)函數(shù)被調(diào)用,在析構(gòu)函數(shù)中,將其引用計(jì)數(shù)減1,如果引用計(jì)數(shù)的值變?yōu)?,則刪除關(guān)聯(lián)的原始指針。

要?jiǎng)h除析構(gòu)函數(shù)中的內(nèi)部原始指針,默認(rèn)情況下,shared_ptr調(diào)用delete()函數(shù),即

delete Pointer;

但是,我們?cè)谖鰳?gòu)函數(shù)中并不總是要使用delete函數(shù),還可能有其他的需求。

如果shared_ptr指向一個(gè)數(shù)組而不是一個(gè)簡(jiǎn)單的指針

std::shared_ptr<int> p3(new int[12]);

在其析構(gòu)函數(shù)中,shared_ptr將會(huì)調(diào)用 delete函數(shù)來(lái)刪除int數(shù)組,而正確的方式是使用 delete []

增加定制deleter到shared_ptr
在這種情況下,我們可以將一個(gè)回調(diào)傳遞給shared_ptr的構(gòu)造函數(shù),該構(gòu)造函數(shù)將會(huì)在其析構(gòu)函數(shù)中被調(diào)用

定制Deleter作為函數(shù)指針

//函數(shù)調(diào)用接收到的指針上的delete[]
void deleter(Sample *x){std::cout<<"DELETE FUNCTION CALLED\n"delete[] x;
}

在shared_ptr的構(gòu)造函數(shù)中傳遞函數(shù)指針,以提供自定義的deleter

//使用定制deleter創(chuàng)建sharedptr
std::shared_ptr<Sample> p3(new Sample[12], deleter);

完整的例子如下:

#include <iostream>
#include <memory>struct Sample {Sample() {std::cout << "CONSTRUCTOR\n";}~Sample() {std::cout << "DESTRUCTOR\n";}
};//在接收到的指針上調(diào)用delte[]的函數(shù)
void deleter(Sample* x) {std::cout << "DELETER FUNCTION CALLED\n";delete[] x;
}int main() {//使用定制的deleter創(chuàng)建shared_ptrstd::shared_ptr<Sample> p3(new Sample[12], deleter);return 0;
}

輸出:

CONSTRUCTOR
CONSTRUCTOR
CONSTRUCTOR
CONSTRUCTOR
CONSTRUCTOR
CONSTRUCTOR
CONSTRUCTOR
CONSTRUCTOR
CONSTRUCTOR
CONSTRUCTOR
CONSTRUCTOR
CONSTRUCTOR
DELETER FUNCTION CALLED
DESTRUCTOR
DESTRUCTOR
DESTRUCTOR
DESTRUCTOR
DESTRUCTOR
DESTRUCTOR
DESTRUCTOR
DESTRUCTOR
DESTRUCTOR
DESTRUCTOR
DESTRUCTOR
DESTRUCTOR
http://aloenet.com.cn/news/34851.html

相關(guān)文章:

  • 珠海網(wǎng)站制作首頁(yè)上線了建站
  • 怎么購(gòu)買網(wǎng)站空間免費(fèi)廣告發(fā)布平臺(tái)
  • 2023年企業(yè)年報(bào)入口推動(dòng)防控措施持續(xù)優(yōu)化
  • wordpress+4.5+多站點(diǎn)手機(jī)百度免費(fèi)下載
  • 網(wǎng)站設(shè)計(jì)怎么做創(chuàng)建自己的網(wǎng)站怎么弄
  • 閔行網(wǎng)站設(shè)計(jì)seo專家是什么意思
  • 六安建設(shè)局網(wǎng)站百度搜索關(guān)鍵詞數(shù)據(jù)
  • bec聽力哪個(gè)網(wǎng)站做的好網(wǎng)站制作公司排名
  • wordpress tag 別名北京優(yōu)化seo公司
  • 石家莊百度推廣家莊網(wǎng)站建設(shè)提高搜索引擎檢索效果的方法
  • 成都網(wǎng)站排名 生客seo自己搭建網(wǎng)站
  • 網(wǎng)站內(nèi)地圖位置怎么做制作app軟件平臺(tái)
  • wordpress如何上傳超過(guò)2m合肥seo網(wǎng)站排名
  • 公安廳網(wǎng)站 做10道相關(guān)題目2022年小學(xué)生新聞?wù)畻l
  • 河南網(wǎng)站制作線上銷售平臺(tái)有哪些
  • 貴州省網(wǎng)站節(jié)約化建設(shè)通知公司網(wǎng)址怎么制作
  • php網(wǎng)站開發(fā)需要什么軟件友情鏈接獲取的途徑有哪些
  • 網(wǎng)站后臺(tái)視頻app開發(fā)公司哪家好
  • 有關(guān)做聚合物電池公司的網(wǎng)站什么是網(wǎng)絡(luò)營(yíng)銷渠道
  • 河南網(wǎng)站推廣網(wǎng)站seo好學(xué)嗎
  • 網(wǎng)站建設(shè)方案市場(chǎng)營(yíng)銷策劃方案
  • 青島網(wǎng)站制作價(jià)格南京百度提升優(yōu)化
  • 蘇州做物流網(wǎng)站電話淘寶店怎么運(yùn)營(yíng)和推廣
  • 用ps如何做網(wǎng)站首頁(yè)網(wǎng)絡(luò)市場(chǎng)調(diào)研的五個(gè)步驟
  • 開花店做網(wǎng)站網(wǎng)絡(luò)營(yíng)銷大賽策劃書
  • 做網(wǎng)站要公安備案嗎百度網(wǎng)盤app怎么打開鏈接
  • 網(wǎng)站3網(wǎng)合一是怎么做的酒吧營(yíng)銷用什么軟件找客源
  • 企業(yè)網(wǎng)站建設(shè)怎么做推銷產(chǎn)品的萬(wàn)能句子
  • 頁(yè)面簡(jiǎn)潔的網(wǎng)站365優(yōu)化大師軟件下載
  • 遼寧品牌建設(shè)促進(jìn)會(huì) 網(wǎng)站網(wǎng)絡(luò)優(yōu)化培訓(xùn)