std::unique_ptr::release

来自cppreference.com
< 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

[编辑] 参阅

返回指向被管理对象的指针
(公开成员函数) [编辑]
替换被管理对象
(公开成员函数) [编辑]