zoukankan      html  css  js  c++  java
  • 理解smart pointer之三:unique_ptr

    unique_ptr最先在boost中被定义,后来被C++标准委员会选中为C++11的feature之一。

    std::unique_ptr is a smart pointer that retains sole ownership of an object through a pointer and destroys that object when the unique_ptr goes out of scope. No two unique_ptr instances can manage the same object.

    The object is destroyed and its memory deallocated when either of the following happens:

    • unique_ptr managing the object is destroyed
    • unique_ptr managing the object is assigned another pointer via operator= or reset().

    The object is destroyed using a potentially user-supplied deleter by calling Deleter(ptr). The deleter calls the destructor of the object and dispenses the memory.

    A unique_ptr may also own no objects, in which case it is called empty.

    There are two versions of std::unique_ptr:

    1. Manages the lifetime of a single object (e.g. allocated with new)
    2. Manages the lifetime of a dynamically-allocated array of objects (e.g. allocated with new[])

    The class satisfies the requirements of MoveConstructible and MoveAssignable, but not the requirements of either CopyConstructible or CopyAssignable. [see C++11 Concepts]

  • 相关阅读:
    SqlServer:创建索引
    SqlServer:使用视图 View
    SqlServer:修改和删除数据
    网络管理:SNMPv1
    《剑指 Offer》学习记录:题 9:用两个栈实现队列
    《剑指 Offer》学习记录:题 27:二叉树的镜像
    团队冲刺9
    团队冲刺8
    团队冲刺7
    团队冲刺6
  • 原文地址:https://www.cnblogs.com/whyandinside/p/3284623.html
Copyright © 2011-2022 走看看