34.share_ptr智能指针共享内存,引用计数

 1 #include <iostream>
 2 #include <memory>
 3 #include <string>
 4 #include <vector>
 5 using namespace std;
 6 
 7 void test()
 8 {
 9         shared_ptr<int> P(new int[10]{ 1,2,3,4,5,6,7,8,9,0 });
10         shared_ptr<int> P1 = P;
11         cout << *P1 << endl;
12         cout << *P << endl;
13         //地址一样
14         cout << &(*P1) << endl;
15         cout << &(*P) << endl;
16 
17         //内存共享,赋值重载或者拷贝构造会使计数+1
18         cout << P.use_count() << endl;
19         //释放内存
20         P.reset();
21 }
22 
23 void main()
24 {
25     test();
26     cin.get();
27 }
原文地址:https://www.cnblogs.com/xiaochi/p/8650947.html