std::unique_ptr<T,Deleter>::release
< cpp | memory | unique ptr
pointer release() noexcept; |
(C++11 起) | |
若存在,则释放被管理对象的所有权。调用后 get() 返回 nullptr 。
调用方负责删除该对象。
参数
(无)
返回值
指向被管理对象的指针,或若无被管理对象则为 nullptr ,即调用前 get() 会返回的值。
示例
运行此代码
#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
参阅
返回指向被管理对象的指针 (公开成员函数) | |
替换被管理对象 (公开成员函数) |