Smart pointers
A smart pointer, definitively, is a class that encapsulates access to a pointer and often manages memory associated with the pointer. If you paid attention, you would have noticed the similarity smart pointers have with pineapples—smart pointers are classes, not pointers, just as pineapples aren't really apples. Moving away from fruit analogies, different types of smart pointers often have additional features like bounds-checking, null-checking, and access control, among others. In C++, smart pointers usually overload the dereference operator (operator->
), which allows any method calls invoked on the smart pointer using operator->
to be bound to the underlying pointer.
Boost includes a set of four different smart pointers with differing semantics. Also, because C++ often uses pointers to identify and manipulate arrays of objects, Boost provides two different smart array templates that encapsulate array access via pointers. In the following sections, we study...