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 }

 

相关文章:

  • 2021-11-18
  • 2021-12-09
  • 2022-12-23
  • 2021-09-19
  • 2022-03-03
  • 2022-01-14
  • 2021-08-14
  • 2022-02-10
猜你喜欢
  • 2022-12-23
  • 2022-12-23
  • 2021-08-07
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
相关资源
相似解决方案