Which C++ smart pointer allows multiple smart pointers to share ownership of the same object?

Options

  • A. unique_ptr
  • B. shared_ptr
  • C. auto_ptr
  • D. More than one of the above
  • E. None of the above

Correct Answer (Detailed Explanation is Below)

B. shared_ptr

Detailed Explanation

Answer: shared_ptr

std::shared_ptr allows multiple smart pointers to share ownership of the same dynamically allocated object.

It uses reference counting. When the last owning shared_ptr is destroyed or released, the managed object is automatically destroyed.

This is useful when an object needs to be owned by multiple parts of a program.