#include #include class Resource { public: Resource() { std::cout << "Resource acquired\n"; } ~Resource() { std::cout << "Resource deleted\n"; } void use() { std::cout<<"Resource use\n"; } }; void test2(std::unique_ptr res) { std::cout<<"test2 using Resourse\n"; res->use(); std::cout<<"about to exit test2()\n"; } // smart pointer automatically deleted here void test() { // allocate memory from the heap: auto res = std::make_unique(); // One way // std::unique_ptr res{new Resource()}; // Another way std::cout<<"test using Resourse\n"; res->use(); // pass pointer to test2 // test2(res); // error: use of deleted function ... <= there's no copy constructor for unique_ptr test2(std::move(res)); // smart pointer already deleted in test2 std::cout<<"about to exit test()\n"; } int main() { std::cout<<"calling test()\n"; test(); std::cout<<"returned from test()\n"; }