赞
踩
shared_ptr的源码:
主要又下列这几个文件组成:main.cpp sp_config.h sp_counted_base.h sp_counted_impl.h
shared_count.h shared_ptr.h
sp_config.h
#ifndef _SP_CONFIG_H
#define _SP_CONFIG_H
#define DISPLAY//配置:在构造和析构的时候是否打印对应的字符串
#endif
shared_ptr.h
#ifndef _SHREAD_PTR_H #define _SHREAD_PTR_H #include"shared_count.h" template<class T> class shared_ptr { public: shared_ptr():px(0), pn() { #ifdef DISPLAY cout<<"Create shared_ptr object."<<endl; #endif } shared_ptr(T *p) : px(p), pn(p) { #ifdef DISPLAY cout<<"Create shared_ptr object."<<endl; #endif } shared_ptr(const shared_ptr<T> &r) : px(r.px), pn(r.pn) { cout<<"Create shared_ptr object."<<endl; } shared_ptr<T>& operator=(const shared_ptr<T> &r) { if(this != &r) { px = r.px; pn = r.pn; } return *this; } ~shared_ptr() { #ifdef DISPLAY cout<<"Free shared_ptr object."<<endl; #endif } public: T& operator*()const { return *px; } T* operator->()const { return px; } T* get()const {return px;} public: long use_count()const { return pn.use_count(); } private: T *px; shared_count pn; }; #endif
shared_count.h
#ifndef _SHREAD_COUNT_H #define _SHREAD_COUNT_H #include"sp_counted_base.h" #include"sp_counted_impl.h" class shared_count { public: shared_count() : pi_( 0) { #ifdef DISPLAY cout<<"Create shared_count object."<<endl; #endif } template<class T> shared_count(T *p) : pi_(new sp_counted_impl<T>(p)) { #ifdef DISPLAY cout<<"Create shared_count object."<<endl; #endif } shared_count(const shared_count &r) : pi_(r.pi_) { if(pi_ != 0) pi_ ->add_ref_copy(); } shared_count & operator=(const shared_count &r) { if(this != &r) { pi_ =r.pi_; if(pi_ != 0) pi_ -> add_ref_copy(); } return *this; } ~shared_count() { #ifdef DISPLAY cout<<"Free shared_count object."<<endl; #endif if(pi_) pi_->release(); } public: long use_count()const { return pi_!=0 ? pi_->use_count() : 0; } private: sp_counted_base * pi_; }; #endif
sp_counted_base.h
#ifndef _SP_COUNTED_BASE_H #define _SP_COUNTED_BASE_H #include"sp_config.h" #include<iostream> using namespace std; class sp_counted_base { public: sp_counted_base() : use_count_(1) { #ifdef DISPLAY cout<<"Create sp_counted_base object."<<endl; #endif } virtual ~sp_counted_base() { #ifdef DISPLAY cout<<"Free sp_counted_base object."<<endl; #endif } public: virtual void dispose() = 0;//要实现多态,所以才采用了父类的指针 public: long use_count()const { return use_count_; } void add_ref_copy() { ++use_count_; } void release() { if(--use_count_ == 0) { dispose(); delete this; } } private: long use_count_; }; #endif
sp_counted_impl.h
#ifndef _SP_COUNTED_IMPL_H #define _SP_COUNTED_IMPL_H #include"sp_counted_base.h" template<class T> class sp_counted_impl : public sp_counted_base { public: sp_counted_impl() : px_(0) { #ifdef DISPLAY cout<<"Create sp_counted_impl object."<<endl; #endif } sp_counted_impl(T *p) : px_(p) { #ifdef DISPLAY cout<<"Create sp_counted_impl object."<<endl; #endif } ~sp_counted_impl() { #ifdef DISPLAY cout<<"Free sp_counted_impl object."<<endl; #endif px_ = 0; } public: void dispose() { delete px_; } private: T *px_; }; #endif
main.cpp
#include<iostream> #include<vld.h> #include"shared_ptr.h" #include"sp_config.h" //#include<string> using namespace std; class Test { public: void fun() {cout<<"Test::fun()."<<endl;} }; void main() { int *p = new int(10); /* 要构造一个ps,需要先构造一个sp_counted_base,再构造一个sp_counted_impl, 再构造一个shared_count */ shared_ptr<int> ps(p); cout<<"ps = "<<*ps<<endl; cout<<"ps use_count = "<<ps.use_count()<<endl; shared_ptr<int> ps1; ps1 = ps; cout<<"ps use_count = "<<ps.use_count()<<endl; shared_ptr<int>ps2 = ps; cout<<"ps use_count = "<<ps.use_count()<<endl; }
下面的图可以帮助大家理解shared_ptr的内存模型。
PS:父类的指针pi_指向了子类对象。
3. 析构的shared_ptr的 时候,首先会析构shared_count类型的对象pn
4. shared_count中的析构函数会调用 其成员pi_的release()函数将引用计数–。如果引用计数变成0的话,调用dispose()函数由于在此类中dispose函数定义的是虚方法,所以会调用子类的dispose()函数方法。
5. 子类的dispose()方法会先释放掉px_所指向的空间。
6. 最后在sp_count_base类中delete this,
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。