.reset 对shared_pointer有什么作用
what does .reset do to a shared_pointer
我想知道.reset()
对共享指针做了什么。它只是将共享指针的引用计数减少 1,如此处所述,还是删除对象的所有引用计数,将计数重置为 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
它释放当前引用。其他引用不受影响。
相关文章:
- archive_read_support_filter_all() 有什么作用?
- 当我们为(;;) 写作时,它做了什么?for 循环中的双分号有什么作用?
- 这个递归函数有什么作用?运行时的复杂性是多少?
- DrawShadow 中的 Elevation 究竟有什么作用?
- 测试"stream >> char"有什么作用?
- "uFlags &= ~CHN_PANNING"命令有什么作用?
- GCC 的 -Wpsabi 选项究竟有什么作用?压制它意味着什么?
- int** a = new int*[n]();这个函数有什么作用?
- C++ 此函数调用之前的(void)有什么作用?
- i+=(i&-i) 有什么作用?它是便携式的吗?
- 这个size_t在 lambda 中有什么作用?C++代码
- 数组下标运算符 ([ ]) 对数组有什么作用?
- 结构之外的节点 * head 有什么作用?
- 重载括号运算符有什么作用?
- clang的"-Ofast"选项在实践中有什么作用,特别是对于与gcc的任何差异?
- std::make_unique_for_overwrite() 对 std::make_unique() 有什么作用?
- 运算符 delete[] 有什么作用?
- 下面的代码有什么作用?c++
- 这个陈述'p = I.ptr<uchar>(i);有什么作用?
- 这个嵌套的名称空间定义有什么作用