1 /* 2 * Copyright (C) 2015-2019 Alibaba Group Holding Limited 3 */ 4 5 #include <iostream> 6 #include <memory> 7 8 using namespace std; 9 10 class resource { 11 public: resource()12 resource() { cout << "resource is create" << endl; } ~resource()13 ~resource() { cout << "resource is delete" << endl; } 14 }; 15 shared_ptr_test(void)16void shared_ptr_test(void) 17 { 18 cout << "shared ptr test start" << endl; 19 shared_ptr<resource> ptest0(new resource); 20 shared_ptr<resource> ptest1 = ptest0; 21 cout << "shared count:" << ptest1.use_count() << endl; 22 if (ptest1.use_count() != 2) { 23 cout << "shared ptr test error" << endl; 24 return; 25 } 26 27 ptest0.reset(); 28 cout << "shared count:" << ptest1.use_count() << endl; 29 30 if (ptest1.use_count() == 1) { 31 cout << "shared ptr test ok" << endl; 32 } else { 33 cout << "shared ptr test error" << endl; 34 } 35 } 36