© The Author(s), under exclusive license to APress Media, LLC, part of Springer Nature 2023
S. DmitrovićModern C++ for Absolute Beginnershttps://doi.org/10.1007/978-1-4842-9274-7_35

35. Smart Pointers

Slobodan Dmitrović1  
(1)
Belgrade, Serbia
 

Smart pointers are pointers that own the object they point to and automatically destroy the object they point to and deallocate the memory once the pointers go out of scope. This way, we do not have to manually delete the object, which was the case with the new and delete operators.

Smart pointers are declared in the <memory> header. We will cover the following smart pointers – unique and shared.

Unique Pointer

A unique pointer called std::unique_ptr is a pointer that owns an object it points to. The pointer cannot ...

Get Modern C++ for Absolute Beginners: A Friendly Introduction to the C++ Programming Language and C++11 to C++23 Standards 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.