std::unique_ptr::release
| pointer release(); |  | (since C++11) | 
|---|
释放托管对象的所有权(如果有的话)。get()回报nullptr打完电话后。
参数
%280%29
返回值
指向托管对象或nullptr如果没有托管对象,即get()打电话之前。
例外
noexcept规格:
noexcept
例
二次
#include <memory>
#include <iostream>
#include <cassert>
 
struct Foo {
    Foo() { std::cout << "Foo\n"; }
    ~Foo() { std::cout << "~Foo\n"; }
};
 
int main()
{
    std::cout << "Creating new Foo...\n";
    std::unique_ptr<Foo> up(new Foo());
 
    std::cout << "About to release Foo...\n";
    Foo* fp = up.release();
 
    assert (up.get() == nullptr);
    std::cout << "Foo is no longer owned by unique_ptr...\n";
 
    delete fp;
}二次
产出:
二次
Creating new Foo...
Foo
About to release Foo...
Foo is no longer owned by unique_ptr...
~Foo二次
另见
| get | returns a pointer to the managed object (public member function) | 
|---|---|
| reset | replaces the managed object (public member function) | 
 © cppreference.com在CreativeCommonsAttribution下授权-ShareAlike未移植许可v3.0。
本文档系腾讯云开发者社区成员共同维护,如有问题请联系 cloudcommunity@tencent.com

