A final warning
The mini-ecosystem of shared_ptr, weak_ptr, and enable_shared_from_this is one of the coolest parts of modern C++; it can give your code the safety of a garbage-collected language while preserving the speed and deterministic destruction that have always characterized C++. However, be careful not to abuse shared_ptr! Most of your C++ code shouldn't be using shared_ptr at all, because you shouldn't be sharing the ownership of heap-allocated objects. Your first preference should always be to avoid heap-allocation altogether (by using value semantics); your second preference should be to make sure each heap-allocated object has a unique owner (by using std::unique_ptr<T>); and only if both of those are really impossible should ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access