std::unique_ptr::release

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

参阅

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

版本历史

  • (当前 | 先前 2017年9月11日 (一) 05:34Fruderica讨论 | 贡献. . (1,142字节) (-561). . (撤销)
  • 当前 | 先前 2014年10月26日 (日) 17:58P12bot讨论 | 贡献 . . (1,703字节) (0). . (Fix some translations) (撤销)
  • 当前 | 先前 2013年7月2日 (二) 09:08P12bot讨论 | 贡献 . . (1,703字节) (-40). . (Use {{lc}}. Update links. Various fixes.) (撤销)
  • 当前 | 先前 2012年11月2日 (五) 14:06P12bot讨论 | 贡献 . . (1,743字节) (+297). . (r2.7.3) (机器人添加:de, en, es, fr, it, ja, pt, ru) (撤销)
  • 当前 | 先前 2012年10月27日 (六) 03:41P12讨论 | 贡献 . . (1,446字节) (0). . (1个修订: Translate from the English version) (撤销)
  • 当前 | 先前 2012年10月26日 (五) 06:00TranslationBot讨论 | 贡献. . (1,446字节) (-44). . (Translated from the English version using Google Translate) (撤销)
  • 当前 | 先前 2012年10月25日 (四) 14:56P12讨论 | 贡献 . . (1,490字节) (0). . (1个修订: Translate from the English version) (撤销)
  • 当前 | 先前) 2012年10月25日 (四) 12:00TranslationBot讨论 | 贡献. . (1,490字节) (+1,490). . (Translated from the English version using Google Translate)