std::unique_ptr::release
来自cppreference.com
< cpp | memory | unique ptr
该页由英文版wiki使用Google Translate机器翻译而来。
该翻译可能存在错误或用词不当。鼠标停留在文本上可以看到原版本。你可以帮助我们修正错误或改进翻译。参见说明请点击这里. |
pointer release(); |
(C++11 起) | |
发布的管理对象(如有)的所有权。
get()
回报nullptr调用后.原文:
Releases the ownership of the managed object if any.
get()
returns nullptr after the call.目录 |
[编辑] 参数
(无)
[编辑] 返回值
如果没有管理对象的指针被管理对象或nullptr,即在调用之前的值,该值将返回
get()
.原文:
Pointer to the managed object or nullptr if there was no managed object, i.e. the value which would be returned by
get()
before the call.[编辑] 例外
[编辑] 示例
#include <memory> #include <iostream> 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(); if (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
[编辑] 另请参阅
返回一个指针,指向被管理对象 原文: returns a pointer to the managed object (公共成员函数) | |
取代管理的对象 (公共成员函数) |