智能指针share_ptr 简单剖析!
2018-01-18 16:30
232 查看
话不多说直接上码!
//shared_ptr的简单实现版本 基于引用记数的智能指针
namespace boost //空间库boost
{
template<class T>
class shared_ptr
{
typedef unsigned longsize_type;
private:
T *px; //指向指针
size_type* pn; //引用计数
public:
explicitshared_ptr(T* p=0) : px(p)
{
pn = new size_type(1);
}
template<typename Y>
shared_ptr(Y* py)
{
pn = newsize_type(1);
px=py;
}
shared_ptr(constshared_ptr& r) : px(r.px)
{
++*r.pn;
pn = r.pn;
}
template<typename Y>
shared_ptr(constshared_ptr<Y>& r)//用于 多态 :一个接口多种实现方法
{
px = r.px;
++*r.pn;
pn = r.pn; //shared_count::op= doesn't throw
}
//重载
shared_ptr& operator=(const shared_ptr& r) throw()
{
if(this== &r) return *this;
dispose(); //处理函数 释放,它里面封装着delete
px = r.px;
++*r.pn;
pn = r.pn;
return *this;
}
template<typename Y>
shared_ptr& operator=(const shared_ptr<Y>& r)//用于多态
{
dispose();
px = r.px;
++*r.pn;
pn = r.pn; //shared_count::op= doesn't throw
return *this;
}
~shared_ptr() { dispose(); }
void reset(T* p=0)
{
if ( px == p ) return;
if (--*pn == 0)
{ delete(px); }
else
{ // allocate newreference
// counter
// fix: prevent leak if new throws
try { pn = new size_type; }
catch (...) {
// undo effect of —*pn above to
// meet effects guarantee
++*pn;
delete(p);
throw;
} // catch
} // allocate newreference counter
*pn = 1;
px = p;
} // reset
reference operator*()const throw(){ return *px; }
pointer operator->()const throw(){ return px; }
pointer get() constthrow(){ returnpx; }
size_type use_count() constthrow()//
{ return *pn; }
bool unique() const throw()//
{ return *pn ==1; }
private:
void dispose() throw()
{
if (--*pn == 0)
{ delete px; delete pn; }
}
}; // shared_ptr
template<typename A,typenameB>
inline bool operator==(shared_ptr<A>const & l, shared_ptr<B> const & r)
{
return l.get() == r.get();
}
template<typename A,typenameB>
inline bool operator!=(shared_ptr<A>const & l, shared_ptr<B> const & r)
{
return l.get() != r.get();
}
}这是它的基本实现原理,接下来来一个它的实际例子,应用很简单!#include<boost/shared_ptr.hpp>
#include<iostream>
using namespace std;
class X
{
public:
X(){cout<< "gouzao"<<endl;}
~X()
{cout << "xigou"<<endl;}
};
int main(void)
{
boost::shared_ptr<X>p1(new X);
cout<<p1.use_count()<<endl;
boost::shared_ptr<X>p2 =p1;
cout<<p2.use_count()<<endl;
p1.reset();
cout<<p2.use_count()<<endl;
p2.reset();
system("pause");//这个可以忽略 因为我使用的是vs2010 防止它闪退 所以加了个终止命令!
return 0;
}
//shared_ptr的简单实现版本 基于引用记数的智能指针
namespace boost //空间库boost
{
template<class T>
class shared_ptr
{
typedef unsigned longsize_type;
private:
T *px; //指向指针
size_type* pn; //引用计数
public:
explicitshared_ptr(T* p=0) : px(p)
{
pn = new size_type(1);
}
template<typename Y>
shared_ptr(Y* py)
{
pn = newsize_type(1);
px=py;
}
shared_ptr(constshared_ptr& r) : px(r.px)
{
++*r.pn;
pn = r.pn;
}
template<typename Y>
shared_ptr(constshared_ptr<Y>& r)//用于 多态 :一个接口多种实现方法
{
px = r.px;
++*r.pn;
pn = r.pn; //shared_count::op= doesn't throw
}
//重载
shared_ptr& operator=(const shared_ptr& r) throw()
{
if(this== &r) return *this;
dispose(); //处理函数 释放,它里面封装着delete
px = r.px;
++*r.pn;
pn = r.pn;
return *this;
}
template<typename Y>
shared_ptr& operator=(const shared_ptr<Y>& r)//用于多态
{
dispose();
px = r.px;
++*r.pn;
pn = r.pn; //shared_count::op= doesn't throw
return *this;
}
~shared_ptr() { dispose(); }
void reset(T* p=0)
{
if ( px == p ) return;
if (--*pn == 0)
{ delete(px); }
else
{ // allocate newreference
// counter
// fix: prevent leak if new throws
try { pn = new size_type; }
catch (...) {
// undo effect of —*pn above to
// meet effects guarantee
++*pn;
delete(p);
throw;
} // catch
} // allocate newreference counter
*pn = 1;
px = p;
} // reset
reference operator*()const throw(){ return *px; }
pointer operator->()const throw(){ return px; }
pointer get() constthrow(){ returnpx; }
size_type use_count() constthrow()//
{ return *pn; }
bool unique() const throw()//
{ return *pn ==1; }
private:
void dispose() throw()
{
if (--*pn == 0)
{ delete px; delete pn; }
}
}; // shared_ptr
template<typename A,typenameB>
inline bool operator==(shared_ptr<A>const & l, shared_ptr<B> const & r)
{
return l.get() == r.get();
}
template<typename A,typenameB>
inline bool operator!=(shared_ptr<A>const & l, shared_ptr<B> const & r)
{
return l.get() != r.get();
}
}这是它的基本实现原理,接下来来一个它的实际例子,应用很简单!#include<boost/shared_ptr.hpp>
#include<iostream>
using namespace std;
class X
{
public:
X(){cout<< "gouzao"<<endl;}
~X()
{cout << "xigou"<<endl;}
};
int main(void)
{
boost::shared_ptr<X>p1(new X);
cout<<p1.use_count()<<endl;
boost::shared_ptr<X>p2 =p1;
cout<<p2.use_count()<<endl;
p1.reset();
cout<<p2.use_count()<<endl;
p2.reset();
system("pause");//这个可以忽略 因为我使用的是vs2010 防止它闪退 所以加了个终止命令!
return 0;
}
相关文章推荐
- 【C++】智能指针auto_ptr简单的实现
- 智能指针之scoped_ptr源码剖析
- C++智能指针简单剖析
- smart_ptr智能指针的简单实现
- 智能指针之scoped_ptr源码剖析
- 智能指针auto_ptr源码剖析
- 智能指针auto_ptr的简单实现
- shared_ptr智能指针源码剖析
- 智能指针之shared_ptr框架源码剖析
- 智能指针auto_ptr简单使用例子
- C++智能指针简单剖析
- STL+auto_ptr智能指针简单分析
- 智能指针的简单剖析和实现
- shared_ptr智能指针的简单实现
- 智能指针share_ptr使用过程中需要注意的几点
- 深入剖析智能指针 shared_ptr
- 【C++】智能指针auto_ptr的简单实现
- shared_ptr智能指针源码剖析
- 关于c++智能指针share_ptr的引用问题
- C++ 智能指针share_ptr