【发布时间】:2014-11-23 01:10:34
【问题描述】:
我想知道.reset() 对共享指针做了什么。
它是像here 提到的那样简单地将共享指针的引用计数减一还是删除对对象的所有引用计数,将计数重置为 0
这是我的代码示例
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();
我很好奇为什么 f.reset() 会将引用计数变为 0,而 vec[1].reset() 将引用计数减少 1
【问题讨论】:
-
您是否阅读过任何文档,或者例如en.cppreference.com/w/cpp/memory/shared_ptr/reset?
标签: c++ shared-ptr