This is a basic question, but I did not find a previous post about it. The title of the following question sounds like it might be the same question as mine, but the question itself does not match the title: is it better to use shared_ptr.reset or operator =?
I am confused about the purpose of the reset()
member function of std::shared_ptr
: what does it contribute in addition to the assignment operator?
To be concrete, given the definition:
auto p = std::make_shared<int>(1);
Are the following two lines equivalent:
p = std::make_shared<int>(5); p.reset(new int(5));
What about these:
p = nullptr; p.reset();
If the two lines are equivalent in both cases, then what is the purpose of reset()
?
EDIT: Let me re-phrase the question to better emphasize its point. The question is: is there a case where reset()
lets us achieve something that is not as easily achievable without it?