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

新手上路,请多包涵

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

原文由 Alex Reynolds 发布,翻译遵循 CC BY-SA 4.0 许可协议

阅读 908
2 个回答

更新

这个答案相当陈旧,因此描述了当时“好”的东西,即 Boost 库提供的智能指针。从 C++11 开始,标准库已经提供了足够的智能指针类型,因此您应该倾向于使用 std::unique_ptrstd::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_ptrstd::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 当您想将对象的生命周期与特定代码块联系起来,或者如果您将其作为成员数据嵌入到另一个对象中时,该对象的生命周期非常有用。该对象一直存在,直到退出包含代码块,或者直到包含对象本身被销毁。

更复杂的智能指针策略涉及对指针的引用计数。这确实允许复制指针。当对象的最后一个“引用”被销毁时,该对象被删除。该政策由 boost::shared_ptrstd::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 都定义了 weak_ptr 来定义对 shared_ptr 的弱(未计数)引用。

原文由 Lloyd 发布,翻译遵循 CC BY-SA 4.0 许可协议

什么是智能指针。

长版,原则上:

https://web.stanford.edu/class/archive/cs/cs106l/cs106l.1192/lectures/lecture15/15_RAII.pdf

现代 C++ 习语:

 RAII: Resource Acquisition Is Initialization.

● When you initialize an object, it should already have
  acquired any resources it needs (in the constructor).

● When an object goes out of scope, it should release every
  resource it is using (using the destructor).

关键:

 ● There should never be a half-ready or half-dead object.
● When an object is created, it should be in a ready state.
● When an object goes out of scope, it should release its resources.
● The user shouldn’t have to do anything more.

原始指针违反 RAII :当指针超出范围时需要用户手动删除。

RAII 解决方案是:

 Have a smart pointer class:
● Allocates the memory when initialized
● Frees the memory when destructor is called
● Allows access to underlying pointer

对于需要复制和共享的智能指针,请使用 shared_ptr:

 ● use another memory to store Reference counting and shared.
● increment when copy, decrement when destructor.
● delete memory when Reference counting is 0.
  also delete memory that store Reference counting.

对于不拥有原始指针的智能指针,请使用weak_ptr:

 ● not change Reference counting.

shared_ptr 用法:

 correct way:
std::shared_ptr<T> t1 = std::make_shared<T>(TArgs);
std::shared_ptr<T> t2 = std::shared_ptr<T>(new T(Targs));

wrong way:
T* pt = new T(TArgs); // never exposure the raw pointer
shared_ptr<T> t1 = shared_ptr<T>(pt);
shared_ptr<T> t2 = shared_ptr<T>(pt);

始终避免使用原始指针。

对于必须使用原始指针的场景:

https://stackoverflow.com/a/19432062/2482283

对于非 nullptr 的原始指针,请改用引用。

 not use T*
use T&

对于可能为 nullptr 的可选引用,请使用原始指针,这意味着:

 T* pt; is optional reference and maybe nullptr.
Not own the raw pointer,
Raw pointer is managed by some one else.
I only know that the caller is sure it is not released now.

原文由 lbsweek 发布,翻译遵循 CC BY-SA 4.0 许可协议

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题