智能指针()是一種抽象的資料類型。在程式設計中,它通常是經由类模板來實作,藉由模板來達成泛型,藉由類別的解構函數來達成自動釋放指標所指向的記憶體或物件。
C++中的智能指针
auto_ptr
auto_ptr這個類別模板被定義在ISO/IEC 14882的第20.4.5章節裡:
namespace std {
template struct auto_ptr_ref {};
template
class auto_ptr {
public:
typedef X element_type;
// 20.4.5.1 construct/copy/destroy:
explicit auto_ptr(X* p =0)throw();
auto_ptr(auto_ptr&)throw();
template auto_ptr(auto_ptr&)throw();
auto_ptr& operator=(auto_ptr&)throw();
template auto_ptr& operator=(auto_ptr&)throw();
auto_ptr& operator=(auto_ptr_ref)throw();
~auto_ptr() throw();
// 20.4.5.2 members:
X& operator*() const throw();
X* operator->() const throw();
X* get() const throw();
X* release() throw();
void reset(X* p =0)throw();
// 20.4.5.3 conversions:
auto_ptr(auto_ptr_ref)throw();
template operator auto_ptr_ref() throw();
template operator auto_ptr() throw();
};
}
unique_ptr
C++11中提供了,定义在头文件中。
C++11新增了move语义,相比copy语义,它能更好的实现值传递.使用的是copy语义,为了向后兼容,C++11没有修改std::auto_ptr,而是引入了新的使用move语义的.
unique_ptr的拷贝构造函数和赋值运算符都声明为deleted,也就是说它不能被拷贝,只能通过来转递它所指向的内存的所有权。
std::unique_ptr p1(new int(5));
std::unique_ptr p2 = p1; // 编译会出错
std::unique_ptr p3 = std::move (p1); // 转移所有权,现在那块内存归p3所有, p1成为无效的指针。
p3.reset(); //释放内存。
p1.reset(); //实际上什么都没做。
依然存在,但在C++11中被标为"弃用".
shared_ptr和weak_ptr
基于Boost库, C++11加入了和.它们最早在TR1中就被引入,但在C++11中,在Boost的基础上又加入了新的功能。
使用引用计数。每一个的拷贝都指向相同的内存。在最后一个析构的时候,内存才会被释放。
std::shared_ptr p1(new int(5));
std::shared_ptr p2 = p1; // 都指向同一内存。
p1.reset(); // 因为p2还在,所以内存没有释放。
p2.reset(); // 释放内存,因为没有shared_ptr指向那块内存了。
使用引用计数,所以有循环计数的问题。为了打破循环,可以使用.顾名思义, weak_ptr是一个弱引用,只引用,不计数。如果一块内存被shared_ptr和weak_ptr同时引用,当所有shared_ptr析构了之后,不管还有没有weak_ptr引用该内存,内存也会被释放。所以不保证它指向的内存一定是有效的,在使用之前需要检查。
std::shared_ptr p1(new int(5));
std::weak_ptr wp1 = p1; // 还是只有p1有所有权。
{
std::shared_ptr p2 = wp1.lock(); // p1和p2都有所有权
if (p2) // 使用前需要检查
{
// 使用p2
}
} // p2析构了,现在只有p1有所有权。
p1.reset(); // 内存被释放。
std::shared_ptr p3 = wp1.lock(); // 因为内存已经被释放了,所以得到的是空指针。
if(p3)
{
// 不会执行到这。
}
外部連結
Sample chapter "[http://www.informit.com/articles/article.aspx?p=25264 Smart Pointers] " from the book [http://www.moderncppdesign.com/ Modern C++ Design: Generic Programming and Design Patterns Applied] * by Andrei Alexandrescu, Addison-Wesley, 2001.
Code example "[http://www.josuttis.com/libbook/cont/countptr.hpp.html countptr.hpp] " from the book [http://www.josuttis.com/libbook/ The C++ Standard Library - A Tutorial and Reference] * by Nicolai M. Josuttis
*"[http://boost.org/libs/smart_ptr/smart_ptr.htm Boost Smart Pointers] "
*Article "[http://www.drdobbs.com/184403837/ The New C++: Smart (er) Pointers] " by Herb Sutter August 01, 2002
*"[http://ootips.org/yonat/4dev/smart-pointers.html Smart Pointers - What, Why, Which?] " by Yonat Sharon
*"[http://dlugosz.com/Repertoire/refman/Classics/Smart%20Pointers%20Overview.html Smart Pointers Overview] " by John M. Dlugosz
- The [http://yasper.sourceforge.net/ YASPER library] Yet Another Smart Pointer implementation in C++
- [http://barrkel.blogspot.com/2008/09/smart-pointers-in-delphi.html Smart Pointers in Delphi]
评论 (0)