目前见到最好的shared_ptr和weak_ptr在这里
附上相关测试代码
#include <iostream> #include <time.h> #include <unordered_map> #include <vector> #include <stack> #include <queue> #include <unordered_set> #include <algorithm> #include <list> #include <memory> using namespace std; #define mySquare(x) (x)*(x) //宏定义对于每个参数都加一个() class Solution { public: //21_0121 最长不含重复字符字串 leetcode 03 int lengthOfLongestSubstring(string s) { if(s.size() == 0) return 0; int res = 0; int left = 0; unordered_map<char,int> unmap; for(int i =0;i<s.size();i++) { left = max(left,unmap[s[i]]); unmap[s[i]] = i +1; res = max(res,i-left+1); } return res; } int lengthOfLongestSubstringTwo(string s) { if(s.size() == 0) return 0; int len = s.size(); int res = 1; for(int i =1;i<s.size();i++) { unordered_set<char> curSet; int left = i-1; int right = i+1; curSet.insert(s[i]); while(left >= 0 && (curSet.find(s[left]) == curSet.end())) { curSet.insert(s[left]); left--; } while(right < len && (curSet.find(s[right]) == curSet.end())) { curSet.insert(s[right]); right++; } int tmpLength = right - left -1; res = max(tmpLength,res); } return res; } }; class CB; class CA { public: CA(){cout<<"CA() called"<<endl;} ~CA(){cout<<"~CA() called"<<endl;} void set_ptr(shared_ptr<CB>& ptr) {m_ptr_b = ptr;} void b_use_count() { cout<<"b use count is "<<m_ptr_b.use_count()<<endl; } void show() { cout<<"this is class CA"<<endl; } void testCA(); private: shared_ptr<CB> m_ptr_b; }; void CA::testCA() { cout<<"This is testCA"<<endl; } class CB { private: weak_ptr<CA> m_ptr_a; public: CB(){cout<<"CB() called"<<endl;} ~CB(){cout<<"~CB() called"<<endl;} void set_ptr(weak_ptr<CA> ptr) { m_ptr_a = ptr; } void show() { cout<<"this is class CB"<<endl; } }; void test_refer_to_eachOther() { shared_ptr<CA> ptr_a(new CA()); shared_ptr<CB> ptr_b(new CB()); cout<<"a use count is "<<ptr_a.use_count()<<endl; cout<<"b use count is "<<ptr_b.use_count()<<endl; ptr_a->set_ptr(ptr_b); ptr_b->set_ptr(ptr_a); cout<<"after set "<<endl; cout<<"a use count is "<<ptr_a.use_count()<<endl; cout<<"b use count is "<<ptr_b.use_count()<<endl; } void test1() { cout<<"begin in test1 function "<<endl; shared_ptr<CA> ptr_1(new CA()); shared_ptr<CA> ptr_2 = ptr_1; weak_ptr<CA> wk_ptr1 = ptr_1; weak_ptr<CA> wk_ptr2(ptr_1); cout<<"ptr1 use_count is "<<ptr_1.use_count()<<endl; } void test2() { cout<<"begin in test2 function"<<endl; shared_ptr<CA> ptr_a(new CA()); shared_ptr<CB> ptr_b(new CB()); weak_ptr<CA> wk_ptr_a = ptr_a; weak_ptr<CB> wk_ptr_b = ptr_b; if(!wk_ptr_a.expired()) { wk_ptr_a.lock()->show(); } if(!wk_ptr_b.expired()) { wk_ptr_b.lock()->show(); } //wk_ptr_a.swap(wk_ptr_b); //编译错误,非同类引用指针类型无法调用swap函数 wk_ptr_b.reset(); if(wk_ptr_b.expired()) cout<<"In test2 wk_ptr_b is invalid"<<endl; cout<<"In test2 last "<<ptr_a.use_count()<<" "<<ptr_b.use_count()<<endl; } int main() { test_refer_to_eachOther(); test1(); test2(); /* int* a = new int(10); std::shared_ptr<int> sPtr(a); std::shared_ptr<int> sPtr1(sPtr); std::shared_ptr<int> sPtr2 = make_shared<int>(10); std::shared_ptr<int> sPtr3(sPtr); int curCount = sPtr.use_count(); cout<<"curCount is "<<curCount<<endl; sPtr3.reset(); cout<<"after reset "<<sPtr.use_count()<<endl; if(sPtr.get()) cout<<"sPtr is not null"<<endl; std::shared_ptr<int> sPtr4 = sPtr; cout<<"*sPtr4 is "<<*sPtr4<<" sPtr.usecount is "<<sPtr.use_count()<<endl; int* b = new int(11); std::shared_ptr<int> sPtr5(b); cout<<"*sPtr5 is "<<*sPtr5<<endl; std::shared_ptr<int> sPtr6 = sPtr5; cout<<"sPtr6 is "<<*sPtr6<<endl; */ time_t now_time = time(NULL); tm* thisLocalTime = localtime(&now_time); cout<<endl; cout<<asctime(thisLocalTime)<<endl; system("pause"); return 0; }
还有一篇shared_ptr简单介绍在这里 https://blog.csdn.net/jacke121/article/details/106805898,但是他写的有些是错误的。比如shared_ptr<int> sptr2 = sptr1; 这个是可行的。
unique_ptr部分待补充。