C++11 has full support for a smart pointer called
std::shared_ptr. Smart pointers keep track of how many references are pointing to a given object. When that reference count goes to zero, the object is destroyed automatically by calling its destructor.
std::shared_ptr is a smart pointer that allows multiple references to be created to the same object.
To quickly understand how to use this smart pointer I created this sample C++ code:
- §12.1: C++ Primer (5th Edition) by Lippman et al.
- §5.2.1, §34.3.2: The C++ Programming Language (4th Edition) by Stroustrup
Tried with: GCC 4.9.1 and Ubuntu 14.04