std::weak_ptr<T>::reset
来自 cppreference.com
void reset() noexcept; |
(自 C++11 起) | |
释放对托管对象的引用。调用后,*this 不再管理任何对象。
内容 |
[编辑] 参数
(无)
[编辑] 返回值
(无)
[编辑] 示例
运行此代码
#include <iostream> #include <memory> int main() { auto shared = std::make_shared<int>(), shared2 = shared, shared3 = shared2; auto weak = std::weak_ptr<int>{shared}; std::cout << std::boolalpha << "shared.use_count(): " << shared.use_count() << '\n' << "weak.use_count(): " << weak.use_count() << '\n' << "weak.expired(): " << weak.expired() << '\n'; weak.reset(); std::cout << "weak.reset();\n" << "shared.use_count(): " << shared.use_count() << '\n' << "weak.use_count(): " << weak.use_count() << '\n' << "weak.expired(): " << weak.expired() << '\n'; }
输出
shared.use_count(): 3 weak.use_count(): 3 weak.expired(): false weak.reset(); shared.use_count(): 3 weak.use_count(): 0 weak.expired(): true
[编辑] 另请参阅
检查引用的对象是否已被删除 (公有成员函数) |