Practice mode C++

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.

Report a problem

Answer checked How we write and check questions

Tip: press A, B, C or D to answer, and N for the next question.

Advertisement

Have a question about this answer?

Your email address will not be published. Required fields are marked *

Links and website addresses are not allowed in comments.