在线文档教程
C++
应用 | Utilities

std::unique_ptr::release

性病::独特[医]PTR::释放

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

二次

另见

getreturns a pointer to the managed object (public member function)
resetreplaces the managed object (public member function)

© cppreference.com

在CreativeCommonsAttribution下授权-ShareAlike未移植许可v3.0。

http://en.cppreference.com/w/cpp/Memory/UNIQUE[医]PTR/释放