Item 42. Smart Pointers

We C++ programmers are a loyal bunch. Whenever we’re faced with a situation that requires a feature the language doesn’t support, we don’t abandon C++ to flirt with some other language; we just extend C++ to support the feature in which we’re interested.

For instance, it’s often the case that you’ll need something that behaves like a pointer, but a built-in pointer type just doesn’t do the job. In those cases, a C++ programmer will use a “smart pointer.” (See also Function Objects [18, 63] for similar observations about function pointers.)

A smart pointer is a class type that is tricked up to look and act like a pointer but that provides additional capability beyond that provided by a built-in pointer. Generally, a smart ...

Get C++ Common Knowledge: Essential Intermediate Programming 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.