Warm tip: This article is reproduced from serverfault.com, please click

c++-什么是智能指针,什么时候应该使用?

(c++ - What is a smart pointer and when should I use one?)

发布于 2008-09-20 00:09:24

什么是智能指针,什么时候应该使用?

Questioner
Alex Reynolds
Viewed
22
6,445 2019-08-25 08:12:08

更新

这个答案相当陈旧,因此描述了当时什么是“好”,即 Boost 库提供的智能指针。从 C++11 开始,标准库提供了足够多的智能指针类型,因此你应该倾向于使用std::unique_ptr,std::shared_ptrstd::weak_ptr

也有std::auto_ptr它非常像一个作用域指针,除了它还有“特殊”危险的复制能力——这也意外地转移了所有权。
它在 C++11 中被弃用并在 C++17 中删除,所以你不应该使用它。

std::auto_ptr<MyObject> p1 (new MyObject());
std::auto_ptr<MyObject> p2 = p1; // Copy and transfer ownership. 
                                 // p1 gets set to empty!
p2->DoSomething(); // Works.
p1->DoSomething(); // Oh oh. Hopefully raises some NULL pointer exception.

旧答案

智能指针是一个包装“原始”(或“裸”)C++ 指针的类,用于管理所指向对象的生命周期。没有单一的智能指针类型,但它们都想以实用的方式抽象一个原始指针。

智能指针应该优于原始指针。如果你觉得你需要使用指针(首先考虑你是否真的这样做了),你通常会想要使用智能指针,因为这可以缓解原始指针的许多问题,主要是忘记删除对象和内存泄漏。

使用原始指针,程序员必须在对象不再有用时显式销毁它。

// Need to create the object to achieve some goal
MyObject* ptr = new MyObject(); 
ptr->DoSomething(); // Use the object in some way
delete ptr; // Destroy the object. Done with it.
// Wait, what if DoSomething() raises an exception...?

相比之下,智能指针定义了有关何时销毁对象的策略。你仍然需要创建对象,但你不再需要担心销毁它。

SomeSmartPtr<MyObject> ptr(new MyObject());
ptr->DoSomething(); // Use the object in some way.

// Destruction of the object happens, depending 
// on the policy the smart pointer class uses.

// Destruction would happen even if DoSomething() 
// raises an exception

使用中最简单的策略涉及智能指针包装对象的范围,例如由boost::scoped_ptr实现std::unique_ptr

void f()
{
    {
       std::unique_ptr<MyObject> ptr(new MyObject());
       ptr->DoSomethingUseful();
    } // ptr goes out of scope -- 
      // the MyObject is automatically destroyed.

    // ptr->Oops(); // Compile error: "ptr" not defined
                    // since it is no longer in scope.
}

请注意,std::unique_ptr无法复制实例。这可以防止指针被多次(错误地)删除。但是,你可以将对其的引用传递给你调用的其他函数。

std::unique_ptr当你想将对象的生命周期与特定代码块联系起来,或者如果你将它作为成员数据嵌入另一个对象中时,s 很有用,即另一个对象的生命周期。该对象一直存在,直到退出包含代码块,或者直到包含对象本身被销毁。

更复杂的智能指针策略涉及对指针进行引用计数。这确实允许复制指针。当对对象的最后一个“引用”被销毁时,该对象将被删除。该政策由boost::shared_ptr实施std::shared_ptr

void f()
{
    typedef std::shared_ptr<MyObject> MyObjectPtr; // nice short alias
    MyObjectPtr p1; // Empty

    {
        MyObjectPtr p2(new MyObject());
        // There is now one "reference" to the created object
        p1 = p2; // Copy the pointer.
        // There are now two references to the object.
    } // p2 is destroyed, leaving one reference to the object.
} // p1 is destroyed, leaving a reference count of zero. 
  // The object is deleted.

当对象的生命周期复杂得多,并且不直接绑定到特定代码段或另一个对象时,引用计数指针非常有用。

引用计数指针有一个缺点——创建悬空引用的可能性:

// Create the smart pointer on the heap
MyObjectPtr* pp = new MyObjectPtr(new MyObject())
// Hmm, we forgot to destroy the smart pointer,
// because of that, the object is never destroyed!

另一种可能性是创建循环引用:

struct Owner {
   std::shared_ptr<Owner> other;
};

std::shared_ptr<Owner> p1 (new Owner());
std::shared_ptr<Owner> p2 (new Owner());
p1->other = p2; // p1 references p2
p2->other = p1; // p2 references p1

// Oops, the reference count of of p1 and p2 never goes to zero!
// The objects are never destroyed!

为了解决这个问题,Boost 和 C++11 都定义了 aweak_ptr来定义对 a 的弱(不可计数)引用shared_ptr