When to use shared_ptr?
Use shared_ptr if you want to share ownership of a resource. Many shared_ptr can point to a single resource. shared_ptr maintains the reference count for this proposal. when all shared_ptr’s pointing to a resource goes out of scope the resource is destroyed.
auto_ptr vs unique_ptr vs shared_ptr vs weak_ptr in C++
Prerequisite – Smart Pointers
Smart Pointer is a pointer-wrapping stack-allocated object. Smart pointers, in plain terms, are classes that wrap a pointer, or scoped pointers.
C++ libraries provide implementations of smart pointers in the following types:
- auto_ptr
- unique_ptr
- shared_ptr
- weak_ptr
They all are declared in a memory header file( #include<memory>) which is used to manage dynamic memory allocation.