Search code examples
c++shared-ptr

what does .reset do to a shared_pointer


I wanted to know what .reset() does to a shared pointer. Does it simply decrement the reference count of a shared pointer by one as mentioned here or does it remove all reference counts to an object resetting the count to 0

This is my code sample here

std::vector<boost::shared_ptr<foo>> vec;
boost::shared_ptr<foo> f(boost::make_shared<foo>()); //ref count 1
vec.push_back(f); //ref count 1
vec.push_back(f); //ref count 3
int a = f.use_count(); //Will return 3
f.reset();        //Will turn the refernece count to 0
vec[1].reset();   //Will reduce the reference count by 1.
a = f.use_count();

I am curious as to why doing f.reset() turns the reference count to 0 while vec[1].reset() reduces the reference count by 1


Solution

  • It releases the current reference. Other references are not affected.