C++智能指針(三)總結

https://blog.csdn.net/nou_camp/article/details/70195795

在上一篇博客中(C++智能指針(二))模擬實現了三種智能指針。?

其中最好的就是shared_ptr,但是這并不代表它就是最完美的,它也有問題,這個問題就是循環引用問題。下面用一段代碼來解釋說明循環引用問題。
#include<memory>
struct  ListNode
{shared_ptr<ListNode> _next;shared_ptr<ListNode> _prev;int _data;ListNode(int x):_data(x), _next(NULL), _prev(NULL){}~ListNode(){cout << "~ListNode()" << endl;}
};void test()
{shared_ptr<ListNode> ap1(new ListNode(10));shared_ptr<ListNode> ap2(new ListNode(20));ap1->_next = ap2;ap2->_prev = ap1;
}
int main()
{test();system("pause");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

這里寫圖片描述

這就是shared_ptr的缺陷–循環引用問題?
這里寫圖片描述

為了解決循環引用的問題,引進了弱指針weak_ptr?
weak_ ptr是一種不控制所指向對象的智能指針,它指向由一個shared_ptr管理的對象。將一個weak_ptr綁定到一個shared_ptr 不會改變shared_ptr的引用計數?
測試代碼如下

#include<iostream>
using namespace std;
#include<memory>
struct  ListNode
{weak_ptr<ListNode> _next;weak_ptr<ListNode> _prev;int _data;ListNode(int x):_data(x){}~ListNode(){cout << "~ListNode()" << endl;}
};void test()
{shared_ptr<ListNode> ap1(new ListNode(10));shared_ptr<ListNode> ap2(new ListNode(20));ap1->_next = ap2;ap2->_prev = ap1;
}
int main()
{test();system("pause");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

仿函數:仿函數(functor),就是使一個類的使用看上去像一個函數。其實現就是類中實現一個operator(),這個類就有了類似函數的行為,就是一個仿函數類了。

template<class T>
struct Less
{bool operator()(const T& l, const T& r){return l < r;}
};
void Test2()
{Less<int>less1;cout << less1(1, 2) << endl;
}
int main()
{Test2();system("pause");return 0;
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19

定制刪除器和空間分配器

#define _CRT_SECURE_NO_WARNINGS
#include<iostream>
using namespace std;
#include<memory>
class FClose
{
public:void operator()(void* ptr){cout << "fclose" << endl;fclose((FILE*)ptr);}
};
class Free
{
public:void operator()(void* ptr){cout << "free" << endl;free(ptr);}
};void test() 
{//定制刪除器shared_ptr<FILE>p1(fopen("test.txt", "w"), FClose());//定制刪除器和分配器shared_ptr<int> p2((int*)malloc(sizeof(int)), Free(), allocator<int>());
}
int main()
{test();system("pause");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

智能指針總結?
智能指針可以提供對動態分配的內存安全而方便的管理,但這建立在正確的使用前提下。?
1.不使用相同的內置指針值初始化(或reset)多個智能指針。?
2.不delete get( )返回的指針。(p.get( )返回p中保存的指針。要小心使用,若智能指針釋放了其對象,返回的指針所指向的對象也就消失了)?
3.不使用get()初始化或reset另一個智能指針。?
4.如果使用get( )返回的指針,記住當最后一個對應的智能指針銷毀后,你的指針就變成無效了。?
5.使用智能指針管理的資源不是new分配的內存,記住傳遞給他一個定制的刪除器。  


本文來自互聯網用戶投稿,該文觀點僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務,不擁有所有權,不承擔相關法律責任。
如若轉載,請注明出處:http://www.pswp.cn/news/383750.shtml
繁體地址,請注明出處:http://hk.pswp.cn/news/383750.shtml
英文地址,請注明出處:http://en.pswp.cn/news/383750.shtml

如若內容造成侵權/違法違規/事實不符,請聯系多彩編程網進行投訴反饋email:809451989@qq.com,一經查實,立即刪除!

相關文章

POJ2114-Boatherds-樹分治

題目描述 Boatherds Inc. is a sailing company operating in the country of Trabantustan and offering boat trips on Trabantian rivers. All the rivers originate somewhere in the mountains and on their way down to the lowlands they gradually join and finally th…

c++11 你需要知道這些就夠了

https://blog.csdn.net/tangliguantou/article/details/50549751c11新特性舉著火把尋找電燈今天我就權當拋磚引玉&#xff0c;如有不解大家一起探討。有部分內容是引用自互聯網上的內容&#xff0c;如有問題請聯系我。T&& 右值引用 std::move 右值引用出現之前我們只能…

HDU5977-Garden of Eden-樹分治+FWT

題目描述 When God made the first man, he put him on a beautiful garden, the Garden of Eden. Here Adam lived with all animals. God gave Adam eternal life. But Adam was lonely in the garden, so God made Eve. When Adam was asleep one night, God took a rib fro…

C++11新特性學習

https://blog.csdn.net/tennysonsky/article/details/778170481、什么是C11C11標準為C編程語言的第三個官方標準&#xff0c;正式名叫ISO/IEC 14882:2011 - Information technology -- Programming languages -- C。在正式標準發布前&#xff0c;原名C0x。它將取代C標準第二版I…

C++ override 關鍵字用法

override關鍵字作用&#xff1a; 如果派生類在虛函數聲明時使用了override描述符&#xff0c;那么該函數必須重載其基類中的同名函數&#xff0c;否則代碼將無法通過編譯。舉例子說明struct Base {virtual void Turing() 0;virtual void Dijkstra() 0;virtual void VNeumann…

Gym - 101981I-MagicPotion-最大流

題目描述 There are n heroes and m monsters living in an island. The monsters became very vicious these days, so the heroes decided to diminish the monsters in the island. However, the i-th hero can only kill one monster belonging to the set Mi. Joe, the st…

c++仿函數 functor

https://www.cnblogs.com/decade-dnbc66/p/5347088.html內容整理自國外C教材先考慮一個簡單的例子&#xff1a;假設有一個vector<string>&#xff0c;你的任務是統計長度小于5的string的個數&#xff0c;如果使用count_if函數的話&#xff0c;你的代碼可能長成這樣&#…

HDU4812-D Tree-樹分治

題目描述 There is a skyscraping tree standing on the playground of Nanjing University of Science and Technology. On each branch of the tree is an integer (The tree can be treated as a connected graph with N vertices, while each branch can be treated as a v…

成為C++高手之實戰項目

https://blog.csdn.net/niu_gao/article/details/51458721 在內存中模擬出一副牌&#xff0c;然后模擬洗牌&#xff0c;發牌等動作。 流程是這樣的&#xff1a;構建一副牌保存到一個數組中—洗牌—創建玩家—向玩家發牌–輸出每個玩家的牌。 #include <stdio.h> #include…

C++中String類的實現

https://www.cnblogs.com/zhizhan/p/4876093.html原文&#xff1a;http://noalgo.info/382.html String是C中的重要類型&#xff0c;程序員在C面試中經常會遇到關于String的細節問題&#xff0c;甚至要求當場實現這個類。只是由于時間關系&#xff0c;可能只要求實現構造函數、…

Ubuntu軟件更新失敗

剛安裝好Ubuntu以后需要將系統的軟件都更新一下&#xff0c;但是遇到一個問題就是下載倉庫信息失敗&#xff0c;大概是這個樣子的錯誤&#xff1a; 經國遇到這樣的問題可以試一下下面這個命令&#xff1a; sudo rm -rf /var/lib/apt/lists/* sudo apt-get update參考網址&…

getsockname函數與getpeername函數的使用

https://www.tuicool.com/articles/V3Aveygetsockname和getpeername函數 getsockname函數用于獲取與某個套接字關聯的本地協議地址 getpeername函數用于獲取與某個套接字關聯的外地協議地址 定義如下&#xff1a;[cpp] view plaincopy#include<sys/socket.h> int gets…

Ubuntu根目錄空間不足

自己在固態硬盤上安裝的Ubuntu&#xff0c;結果只用了一天就顯示磁盤空間不足。查看空間以后發現Ubuntu自己安裝的時候默認給根目錄分配的是10GB,然而我們下載的軟件以及環境等一般都安裝在根目錄空間下&#xff0c;尤其是/usr目錄所占的空間很大。 不得已我在網上查找了如何給…

Linux命令【一】基本命令

shell命令和bash命令相同&#xff0c;指的是命令解析器 快捷鍵 history 所有的歷史命令ctrl P 向上滾動命令 ctrl N 向下滾動命令 ctrlB將光標向前移動 ctrlF將光標向后移動 ctrlA移動到命令行頭部 ctrlE移動到命令行尾部 光標刪除操作&#xff1a;刪除光標前面字符ctrlh或…

The MySQL server is running with the --skip-grant-tables option so it cannot execute this statement

http://www.jb51.net/article/119654.htmThe MySQL server is running with the --skip-grant-tables option so it cannot execute this statement 意思貌似MYSQL還運行在 --skip-grant-tables模式&#xff0c;如何讓他回到原來的模式 第一種方法&#xff1a;原來在mysql.ini文…

解決Ubuntu“下載額外數據文件失敗 ttf-mscorefonts-installer”的問題

參考博客&#xff1a;傳送門 下載[ttf-mscorefonts-installer.zip](https://pan.baidu.com/s/1i5rLfMH) 密碼: h76g 然后解壓到下載的目錄&#xff0c;在當前目錄執行命令&#xff1a; sudo dpkg-reconfigure ttf-mscorefonts-installer這條命令手動指定文件夾的位置,重新配置…

【C語言】單鏈表的相關熱點面試題(包括:從尾到頭打印,逆置,冒泡,尋找中間節點,倒數k節點)

https://blog.csdn.net/hanjing_1995/article/details/51539599從尾到頭打印單鏈表[cpp] view plaincopyvoid FromTailToHeadPrint(SListNode*& head) { stack<SListNode*> s; SListNode* cur head; while (cur) { s.push(cur); …

Linux命令【二】終端+Vim

需要先安裝net-tools ifconfig eth0 網卡&#xff0c;硬件地址為MAC 地址&#xff0c;網卡編號&#xff0c;絕對不會重復 lo 回環地址 測試兩臺主機之間能否通信&#xff1a;ping IP或域名 [-c 4//回饋四條信息 -i//每隔多少秒回饋一次] 得到域名對應的IPnslookup 域名得到域…

Linux如何將文件中內容放到粘貼板上

沒有找到如何在vim中將內容復制到粘貼板上&#xff0c;只找到了使用另一個軟件進行操作。 首先安裝xsel sudo apt-get install xsel # 將剪切板中的內容輸出到文件 echo $(xsel --clipboard) >> a.txt# 將文件的內容復制到剪切板 cat a.txt | xsel --clipboard

【C語言】str類與men庫函數的實現(如:strcpy,strcmp,strstr,strcat,memmove,memcpy)

https://blog.csdn.net/hanjing_1995/article/details/51539583strcpy拷貝源字符串到子字符串&#xff0c;包括‘\0’。代碼實現&#xff1a;[cpp] view plaincopychar* strcpy(char* dst,const char* src) { assert(src); char* ret dst; while (*src) …