When to use a weak pointer?
Show answer and explanation
Use std::weak_ptr as a non-owning observer of an object managed by shared_ptr, especially to break ownership cycles. Call lock() and test the returned shared_ptr before accessing the object.
Use std::weak_ptr as a non-owning observer of an object managed by shared_ptr, especially to break ownership cycles. Call lock() and test the returned shared_ptr before accessing the object.
Connect the conclusion directly to the type, lifetime, ownership, overload-resolution rule, or library contract present in the question. State implementation dependence or a violated precondition explicitly instead of merely guessing output.