Member-only story
Effective C++
C++ Smart Pointer Explained Through Intuitive Visuals
Part 3 of my C++ pointer series
6 min readApr 27, 2022
· Overview
· Item 18: Unique_ptr for exclusive-ownership resource management.
∘ An Exclusive Relationship
∘ Raw Pointers
∘ The smart pointer
∘ Custom deleters
∘ Custom interface
∘ Tips for Smart Pointers
· Item 19: Shared_ptr for shared-ownership resource management.
∘ Analogy: Simple reference counting
∘ Detour: Physical class layout
· Item 21: Prefer unique_ptr and shared_ptr to direct use of new.
∘ Goal: No raw new or delete
∘ Preferred Usage
· Conclusion
· References
Overview
Let us continue our discussion of smart pointers: an object in C++ that reaps the benefits of raw pointers while overcoming their shortcomings. This is the third part in the series (see Part I [1] and Part II [2]).
We visually explore concepts of smart pointers in C++. We will reference a book all C++ developers should be familiar with, i.e., Effective Modern C++ by Scott Meyers [3].