什么是智能指针,什么时候应该使用? [英] What is a smart pointer and when should I use one?

查看:34
本文介绍了什么是智能指针,什么时候应该使用?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

What is a smart pointer and when should I use one?

推荐答案

UPDATE

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

This answer is rather old, and so describes what was 'good' at the time, which was smart pointers provided by the Boost library. Since C++11, the standard library has provided sufficient smart pointers types, and so you should favour the use of std::unique_ptr, std::shared_ptr and std::weak_ptr.

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

There was also std::auto_ptr. It was very much like a scoped pointer, except that it also had the "special" dangerous ability to be copied — which also unexpectedly transfers ownership.
It was deprecated in C++11 and removed in C++17, so you shouldn't use it.

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++ 指针的类,用于管理所指向对象的生命周期.没有单一的智能指针类型,但它们都试图以实用的方式抽象出一个原始指针.

A smart pointer is a class that wraps a 'raw' (or 'bare') C++ pointer, to manage the lifetime of the object being pointed to. There is no single smart pointer type, but all of them try to abstract a raw pointer in a practical way.

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

Smart pointers should be preferred over raw pointers. If you feel you need to use pointers (first consider if you really do), you would normally want to use a smart pointer as this can alleviate many of the problems with raw pointers, mainly forgetting to delete the object and leaking memory.

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

With raw pointers, the programmer has to explicitly destroy the object when it is no longer useful.

// 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...?

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

A smart pointer by comparison defines a policy as to when the object is destroyed. You still have to create the object, but you no longer have to worry about destroying it.

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.

The simplest policy in use involves the scope of the smart pointer wrapper object, such as implemented by boost::scoped_ptr or 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 实例不能被复制.这可以防止指针被多次(错误地)删除.但是,您可以将对其的引用传递给您调用的其他函数.

Note that std::unique_ptr instances cannot be copied. This prevents the pointer from being deleted multiple times (incorrectly). You can, however, pass references to it around to other functions you call.

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

std::unique_ptrs are useful when you want to tie the lifetime of the object to a particular block of code, or if you embedded it as member data inside another object, the lifetime of that other object. The object exists until the containing block of code is exited, or until the containing object is itself destroyed.

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

A more complex smart pointer policy involves reference counting the pointer. This does allow the pointer to be copied. When the last "reference" to the object is destroyed, the object is deleted. This policy is implemented by boost::shared_ptr and 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.

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

Reference counted pointers are very useful when the lifetime of your object is much more complicated, and is not tied directly to a particular section of code or to another object.

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

There is one drawback to reference counted pointers — the possibility of creating a dangling reference:

// 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!

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

Another possibility is creating circular references:

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 的弱(不可计数)引用.

To work around this problem, both Boost and C++11 have defined a weak_ptr to define a weak (uncounted) reference to a shared_ptr.

这篇关于什么是智能指针,什么时候应该使用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆