Assuming standard headers and using namespace std, what does this C++ code print? auto p = make_shared<int>(7); auto q = p; { auto r = p; cout << p.use_count(); } cout << p.use_count();
Correct answer B. 32
Explanation
Each copy of a shared_ptr shares ownership and increments the reference count. With p, q and r alive the count is 3. When the inner block ends, r is destroyed and the count drops to 2. The managed int is deleted only when the last owning shared_ptr goes away, which is the core idea of shared ownership.
Answer checked How we write and check questions
Discussion