shared_ptr and weak_ptr c++ tutorial
shared_ptr is a reference counted c++ 11 smart pointer which supports shared ownership model. reference counted means it keeps track of how many smart pointers are pointing to the dynamically created object ( manged object ). The managed object is destroyed when reference count becomes zero.
- An empty shared_ptr does not own any resources and has no control block. control block holds the reference count.
- we can copy or assign a shared_ptr to another shared_ptr.
using namespace std;
int main ()
shared_ptr < A > obj (new A); // ref count of obj is 1
shared_ptr < A > pobj = obj; // obj and pobj shared
//ownership of A
weak_ptr does not control life time of the managed object.
Note: The article is yet to be completed.