is the only smart pointer class available in C++98. Alas, since this class is neither assignable nor copy-constructible, creating containers of auto_ptr objects is illegal
. This limitation has been a source of frustration and confusion for years. At last, the Library Extensions Technical Report 1 (TR1 for short) rectifies this embarrassment, adding a new
smart pointer class called shared_ptr
to the standard header <memory>
. The following sections explain how to use shared_ptr
to automate resource management. You will also learn how shared_ptr
can simplify programming tasks that I've shown here beforesuch as simulating heterogeneous containers and implementing the Pimpl idiom.
How to combine smart pointers with STL containers and algorithms without risking undefined behavior?
Use the std::tr1::shared_ptr class.