Smart Pointers — Automated Ownership of Dynamic Variables
Previous exercises have demonstrated that an owned pointer can be wrapped in a class instance, which can delete the dynamic variables owned by the pointer when the class is destroyed. It is possible to take this design a step further and create a class consisting only of an owned pointer to a dynamic variable. Such an object is called a smart pointer.
The design of the smart pointers in the C++ standard library takes advantage of most of the advanced features of C++, including operator functions, template metaprogramming, move semantics, variadic templates, and perfect forwarding. The design is too advanced to cover in this brief course. However, the result is a thing that looks and acts much like a raw pointer but deletes its owned dynamic variable when the smart pointer is destroyed.
unique_ptr<>
unique_ptr<>
is a smart pointer template class that owns a dynamic variable. In C++, a template class...