Step 5 of 5
weak_ptr
A std::weak_ptr watches a shared object without owning it. It doesn't keep the object alive. To use it, call lock(), which returns a shared_ptr that's empty if the object is already gone.
std::weak_ptr<Player> target = some_shared_player;
if (auto p = target.lock()) {
// p is a valid shared_ptr here
} else {
// the player was deleted
}
This breaks reference cycles (two shared_ptrs pointing at each other would never be freed) and models "I'm tracking this, but I don't own it".
Your turn: write std::string describe(const std::weak_ptr<std::string>& w) returning "tracking NAME" if the object is alive and "lost" otherwise.