C++ 标准库(STL)中

C++ 98

C++ 11

  1. unique_ptr
  2. weak_ptr
  • Class shared_ptr 实现共享式拥有(shared ownership)概念。多个智能指针指向相同对象,该对象和其相关资源会在 “最后一个 reference 被销毁” 时被释放。为了在结构较复杂的情景中执行上述工作,标准库提供 weak_ptr、bad_weak_ptr 和 enable_shared_from_this 等辅助类。
  • Class unique_ptr 实现独占式拥有(exclusive ownership)或严格拥有(strict ownership)概念,保证同一时间内只有一个智能指针可以指向该对象。你可以移交拥有权。它对于避免内存泄漏(resource leak)——如 new 后忘记 delete ——特别有用。
shared_ptr
weak_ptr
  • 可打破环状引用(cycles of references,两个其实已经没有被使用的对象彼此互指,使之看似还在 “被使用” 的状态)的问题
unique_ptr
  • unique_ptr 用于取代 auto_ptr
auto_ptr
auto_ptr 与 unique_ptr 比较
  • auto_ptr 对象不能管理数组(析构调用 ),unique_ptr 可以管理数组(析构调用 );