(C++) Smart pointer
January 11, 2018 ยท View on GitHub
A smart pointer is a container that manages a pointer.
Boost.Smart_ptr is the Boost smart pointer library.
List of smart pointers (incomplete)

boost::scoped_ptr
boost::shared_ptr
boost::weak_ptr
QPointer
std::auto_ptr
std::shared_ptr
std::tr1::shared_ptr
std::unique_ptr
Examples
- Smart pointers example 1: do the classes check for uninitialized pointers?
- Smart pointers example 2: reset smart pointers to nullptr
Advice
- Prefer to use smart pointers over "naked" new and delete [1-6]
- Prefer smart pointers to garbage collection [7]
- Polymorphic types must always be passed by reference or (smart) pointer [8]
References
- Scott Meyers. Effective C++ (3rd edition). ISBN: 0-321-33487-6. Item 13: 'Use objects to manage resources'.
- Scott Meyers. Effective C++ (3rd edition). ISBN: 0-321-33487-6. Item 17: 'Store newed objects in smart pointers in standalone statements'
- Herb Sutter, Andrei Alexandrescu. C++ coding standards: 101 rules, guidelines, and best practices. 2005. ISBN: 0-32-111358-6. Chapter 13: 'Ensure resources are owned by objects. Use explicit RAII and smart pointers.
- Bjarne Stroustrup. The C++ Programming Language (4th edition). 2013. ISBN: 978-0-321-56384-2. Page 19, 1.3.2 'Advice', item 3: 'Avoid 'naked' new and delete'
- Bjarne Stroustrup. The C++ Programming Language (4th edition). 2013. ISBN: 978-0-321-56384-2. Chapter 11.6. Advice. page 303: '[4] Avoid "naked new" and "naked delete"'
- Bjarne Stroustrup. The C++ Programming Language (4th edition). 2013. ISBN: 978-0-321-56384-2. Chapter 21.4. Advice. page 640: '[1] Use unique_ptr or shared_ptr to avoid forgetting to delete objects created using new'
- Bjarne Stroustrup. A tour of C++. 2014. ISBN: 978-0-321-958310. Chapter 11.7.9, page 131: 'Prefer smart pointers to garbage collection'
- Gottschling, Peter. Discovering Modern C++: An Intensive Course for Scientists, Engineers, and Programmers. Addison-Wesley Professional, 2015. Chapter 6.1.3: 'Polymorphic types must always be passed by reference or (smart) pointer!'