简体   繁体   English

.reset对shared_pointer做了什么

[英]what does .reset do to a shared_pointer

I wanted to know what .reset() does to a shared pointer. 我想知道.reset()对共享指针的作用。 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 它简单地由一个递减一个共享指针的引用计数如所提到的在这里或者它删除所有引用计数到对象重置的计数为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 我很好奇为什么执行f.reset()会将引用计数变为0而vec[1].reset()将引用计数减少1

It releases the current reference. 释放当前参考。 Other references are not affected. 其他参考不受影响。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM