Smart Pointers

A program that uses new to allocate memory from the heap (or free store) should use delete to free that memory when it no longer is needed. Earlier, C++ introduced the auto_ptr smart pointer to help automate the process. Subsequent programming experience, particularly with the STL, indicated that something more sophisticated was needed. Guided by the experience of programmers and by solutions provided by the BOOST library, C++11 deprecates auto_ptr and introduces three new smart pointer types: unique_ptr, shared_ptr, and weak_ptr. Chapter 16 discusses the first two.

All the new smart pointers have been designed to work with STL containers and with move semantics.

Get C++ Primer Plus now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.