Skip to content

Latest commit

 

History

History
6 lines (5 loc) · 649 Bytes

File metadata and controls

6 lines (5 loc) · 649 Bytes

智能指针I

  • unique_ptr 是一个独占所有权的智能指针,它的特性包括但不限于自动管理动态分配的内存,确保对象只有一个所有者、在作用域结束时自动释放内存以及零开销抽象(几乎没有性能损失)
  • unique_ptr 的拷贝构造函数被隐式删除(= delete),因此不支持拷贝语义,但支持移动语义
  • 如果函数只是临时使用对象,使用引用传递 std::unique_ptr<T> & ;如果函数需要接管所有权,才使用移动传递(std::move(ptr)
  • make_unique 用于创建 unique_ptr 对象,相比直接构造,是更推荐的方式