Core · Module 5 · Lesson 4 of 6
Smart Pointers: unique_ptr, shared_ptr and weak_ptr
Replace raw owning pointers with unique ownership, shared ownership, and non-owning observers, using make_unique and make_shared throughout.
By the end you can
- Transfer exclusive ownership with unique_ptr and move semantics
- Share ownership with shared_ptr while tracking the use count
- Break cycles with weak_ptr and observe without extending lifetime
Builds on Copy Semantics: Deep Copies and the Rule of Three.
01 · A question first
A checkout function allocates a Member, throws on a missing book, and leaks the member on every failure path. Adding a delete before each throw helps until the next early return appears. How can the pointer clean itself up on every path, including the ones you have not written yet?
Make a guess before you read on.
02 · The idea
Members only
Unlock the full lesson
This lesson is part of the membership. Join to read it in full, along with every other premium deep dive.
What's inside
- Why raw ownership resists inspection
- How exclusive ownership moves
- How shared ownership counts
- What it costs and when to avoid sharing
- Original cast examples
- Where it is used
03 · Practice
Asked in interviews
Something wrong with this lesson? Report a problem