Challenge 2: Observing a shared_ptr's Reference Count Change — Possible Solution ==================================================================== #include #include int main() { std::shared_ptr a = std::make_shared(42); std::cout << "After creating a: " << a.use_count() << std::endl; std::shared_ptr b = a; std::cout << "After creating b: " << a.use_count() << std::endl; { std::shared_ptr c = a; std::cout << "After creating c: " << a.use_count() << std::endl; } // c goes out of scope here std::cout << "After c's scope ends: " << a.use_count() << std::endl; return 0; } Output: After creating a: 1 After creating b: 2 After creating c: 3 After c's scope ends: 2 WHY THIS WORKS AS AN ANSWER ------------------------------ Each new shared_ptr copy that jointly owns the same object increments the shared reference count by one (1 -> 2 -> 3), and when c goes out of scope at the closing brace, its own destructor decrements the count back down to 2 -- confirming shared_ptr's reference-counting mechanism directly, and showing that the underlying int is only ever actually deleted once the count would reach zero (which never happens here, since a and b are both still alive).