什么是智能指针,什么时候应该使用?
更新
这个答案相当陈旧,因此描述了当时什么是“好”,即 Boost 库提供的智能指针。从 C++11 开始,标准库提供了足够多的智能指针类型,因此你应该倾向于使用std::unique_ptr
,std::shared_ptr
和std::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
。
你的意思是
std::auto_ptr<MyObject> p1 (new MyObject());
代替std::auto_ptr<MyObject> p1 (new Owner());
?很棒的答案。如果为 c++11 更新它会很好。我发现这个答案正在寻找有关新 11 标准的信息,如果未来的访问者能够找到更新的信息,那就太好了。我知道 auto_ptr 已被弃用。我相信 shated_ptr 和 weak_ptr 存在于描述中,我认为 scoped_ptr 现在是标准中的 unique_ptr。如果这是真的,请问这个答案可以更新吗?
要说创建悬空引用的可能性是引用计数指针的一个缺点是绝对疯狂的。可能的悬空引用是任何 C++ 指针的缺点。事实上,这正是智能指针旨在减轻的缺点。
如果您声明一个指向智能指针的指针(如示例中所做的那样),您会故意放弃智能指针的所有好处。这不是缺点或设计缺陷,这是可以想象的最愚蠢的用法。
const std::auto_ptr
如果您坚持使用 C++03,A可以安全使用。在我使用 C++11 之前,我经常将它用于 pimpl 模式。