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
二次
另见
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。