If we make them smarter they will become self-aware
Smart pointers are objects that store pointers to dynamically allocated objects. The purpose of them is to manage to lifetime of the object being pointed to. The smart pointer owns the object it points to and to access the object you must use the smart pointer. When the smart pointer goes out of scope it checks that no one us still using the object it points to and if so it deletes it.
std::unique_ptr - C++11 - For exclusive-ownership of a resource
- Exploring std::unique_ptr - 2016
std::shared_ptr - C++11 - For shared-ownership of a resource
std::weak_ptr - C++11
A weak_ptr holds a reference to an object managed by a shared_ptr, but does not contribute to the reference count. To access the object one use the lock() function on the weak_ptr to create a shared_ptr.
std::auto_ptr - C++98, deprecated since C++11
This was the first try of making a smart pointer object in C++. Deprecated in C++11 when the better working ways above was created.
- std::make_shared vs. the Normal std::shared_ptr Constructor - 2018
- Top 10 dumb mistakes to avoid with C++ 11 smart pointers - 2016
- CppCon 2016: Herb Sutter “Leak-Freedom in C++... By Default" - 2016
- Lesson #4: Smart Pointers - 2012
- C++ Core Guidelines: Rules to Resource Management - 2017
- Smartness overload
- Smartness overload – addendum