#include #include class Resource { public: int value; Resource() { std::cout << "Resource acquired\n"; } ~Resource() { std::cout << "Resource deleted\n"; } void use() { std::cout<<"Resource use; value "< test1() { // allocate memory from the heap: // std::shared_ptr res(new Resource); // one way auto res = std::make_shared(); // another way std::cout<<"test1() using Resource\n"; res->use(); res->value = 1; std::cout<<"about to exit test1()\n"; return res; } void test2(std::shared_ptr res) { std::cout<<"test2() using Resource res\n"; res->value = 2; res->use(); std::cout<<"about to exit test2()\n"; } void test3( Resource& res) { std::cout<<"test3() using Resource res\n"; res.value = 3; res.use(); std::cout<<"about to exit test3()\n"; } int main() { std::shared_ptr shared_res2; { auto shared_res1 = test1(); std::cout<<"returned from test1()\n"; std::cout<<"shared_res1 use_count = "<