boost库的weak_ptr剖析
来源:互联网 发布:asp.net php学那个好 编辑:程序博客网 时间:2024/06/08 20:16
weak_ptr最大的特点就是他是一个弱指针,他和shared_ptr的区别重点突出于“弱”,因为它是弱指针,所以它仅有几个接口,仅能完成很少的工作,它可以从一个shared_ptr或weak_ptr对象构造,获得对资源的观测权,它没有共享资源,所以他对象的创建不会引起指针引用计数的增加,它对象的析构不会引起计数器的减少(这也是它能够使得shared_ptr打破循环计数的重要依据)
http://blog.csdn.net/xiejingfa/article/details/50772571
这位同学的博客有关于weak_ptr如何打破shared_ptr指针的实例,有想了解的可以去看一下,我这里主要是对源码的剖析。
(1)weak_ptr可以看作是shared_ptr的一个辅助指针,它可以通过shared _ptr的构造函数或者是成员函数lock来实现向shared_ptr的转换过程。
(2)当最后一个shared_ptr指向的对象被释放的时候,通过weak_ptr去获取shared_ptr指向被删除对象的行为会失败,构造函数会抛出一个异常类型boost::bad_weak_ptr,然后weak_ptr::lock会返回一个空的shared_ptr(weak_ptr的一个重要用途是获得this指针的shared_ptr,使对象能够生产shared_ptr管理自己,调用的就是lock函数来帮助实现上述做法)、
(3)px代表数据类型,pn代表几份拷贝
函数框架::由于weak_ptr针对的都是shared_ptr实现的,所以他的接口和函数以及面向范围都要小的多,所以框架规模也就小很多
namespace boost { template<class T> class weak_ptr { public: typedef T element_type; weak_ptr(); template<class Y> weak_ptr(shared_ptr<Y> const & r); weak_ptr(weak_ptr const & r); template<class Y> weak_ptr(weak_ptr<Y> const & r); ~weak_ptr(); weak_ptr & operator=(weak_ptr const & r); template<class Y> weak_ptr & operator=(weak_ptr<Y> const & r); template<class Y> weak_ptr & operator=(shared_ptr<Y> const & r); long use_count() const; bool expired() const; shared_ptr<T> lock() const; void reset(); void swap(weak_ptr<T> & b); }; template<class T, class U> bool operator<(weak_ptr<T> const & a, weak_ptr<U> const & b); template<class T> void swap(weak_ptr<T> & a, weak_ptr<T> & b);}
至于源代码我就不贴上来了,到处都有,重点就是你搞懂了他的框架和函数接口的功用,这样你就能够相对简单的搞懂源代码
- boost库的weak_ptr剖析
- boost::weak_ptr
- boost库的shared_ptr剖析
- 【Boost】boost库中智能指针——weak_ptr
- boost库在工作(11)引用计数的智能指针weak_ptr
- boost库在工作(11)引用计数的智能指针weak_ptr
- boost::weak_ptr和enable_shared_from_this
- boost::weak_ptr和enable_shared_from_this
- boost::weak_ptr和enable_shared_from_this
- boost::weak_ptr和enable_shared_from_this
- boost学习之-weak_ptr
- 智能指针boost::weak_ptr
- boost::weak_ptr和enable_shared_from_this
- boost::weak_ptr和enable_shared_from_this
- boost::weak_ptr和enable_shared_from_this
- boost weak_ptr使用
- 关于boost的shared_ptr以及weak_ptr的研究
- 几种智能指针的比较(std::auto_ptr、boost::scoped_ptr、boost::shared_ptr、boost::weak_ptr)
- 教你实现简单的极光推送-Eclipse
- Java--继承与多态,抽象编程,接口与多态,接口回掉接口编程
- HDFS too many open files,fd limit的问题(二)
- Http协议
- Android安全之WebViewUXSS漏洞
- boost库的weak_ptr剖析
- 方法的定义
- 抽象类(abstract class)与接口(interface)的异同
- Linux常用命令学习总结(一)
- LeetCode 7. Reverse Integer
- HBase操作(Shell与Java API)
- HTML5新特性结合PHP实现多图片格式转换功能 欢迎讨论
- unity - 二进制文件操作-存储与读取
- java工程如何打jar包和war包