Namespaces
Variants

std::weak_ptr<T>:: reset

From cppreference.net
Memory management library
( exposition only* )
Allocators
Uninitialized memory algorithms
Constrained uninitialized memory algorithms
Memory resources
Uninitialized storage (until C++20)
( until C++20* )
( until C++20* )
( until C++20* )

Garbage collector support (until C++23)
(C++11) (until C++23)
(C++11) (until C++23)
(C++11) (until C++23)
(C++11) (until C++23)
(C++11) (until C++23)
(C++11) (until C++23)
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

参阅

检查被引用的对象是否已被删除
(公开成员函数)