Chapter 2. Smart Pointers
I shot an arrow into the air, It fell to earth, I know not where.
— “The Arrow and the Song”HENRY WADSWORTH LONGFELLOW
Almost every nontrivial application controls some resource whose lifetime is intermediate between the lifetime of an auto
object, which exists only while the function that defines it is executing, and the lifetime of a global object, which exists as long as the program is running. The most common resource with this sort of intermediate lifetime is memory, and experienced programmers are well aware of the problems that arise from errors in handling dynamically allocated memory. The TR1 library provides a pair of templates—shared_ptr
, discussed in Section 2.4, and weak_ptr
, discussed in Section 2.5—that ...
Get The C++ Standard Library Extensions A Tutorial and Reference 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.