首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 开发语言 > C++ >

请教智能指针是怎么实现的

2012-02-10 
请问智能指针是如何实现的请问智能指针是如何实现的[解决办法]File: shared_ptr.h///////////////////////

请问智能指针是如何实现的
请问智能指针是如何实现的

[解决办法]
File: shared_ptr.h

///////////////////////////////////////////////////////////////////////////

//

// template class shared_ptr

// 2004-6-12

// Chengliang Shan

// simouse@126.com

//

///////////////////////////////////////////////////////////////////////////

#ifndef SHARED_PTR_H_

#define SHARED_PTR_H_



namespace clshan{



template <class T>

class shared_ptr;



template <class T>

class shared_data

{

private:

friend class shared_ptr <T> ;



explicit shared_data(T* pT):_M_ptr(pT) {

_M_nCount = 1;

}



~shared_data() {

delete _M_ptr;

}



void operator++ () {

++_M_nCount;

}



operator-- () {

--_M_nCount;

if (_M_nCount == 0) {

delete this;

}

}



T& operator* () const {

return *_M_ptr;

}



T* operator-> () const {

return _M_ptr;

}



bool operator== (T* pT) const {

return _M_ptr == pT;

}



T* get() {

return _M_ptr;

}



int use_count() {

return _M_nCount;

}



private:

T* _M_ptr;

unsigned int _M_nCount;

};



template <class T>

class shared_ptr

{

typedef shared_data <T> element;

public:

explicit shared_ptr(T* pT):_M_pD(NULL) {

_M_pD = new element(pT);

}



explicit shared_ptr():_M_pD(NULL){

}



// copy constructor

shared_ptr(const shared_ptr <T> & rT) {

_M_pD = rT.get_element();

if (_M_pD != NULL) {

++(*_M_pD);

}

}



~shared_ptr() {

if (_M_pD != NULL) {

--(*_M_pD);

}

}



// assignment operator

shared_ptr <T> & operator = (shared_ptr <T> & rT) {

if (_M_pD != NULL) {



--(*_M_pD);

}

_M_pD = rT.get_element();

if (_M_pD != NULL){

++(*_M_pD);

}

return *this;

}



T& operator* () const {

return _M_pD-> operator *();

}



T* operator-> () const {

return _M_pD-> operator -> ();

}



bool operator== (shared_ptr <T> & rT) const {

return rT.get_element() == _M_pD;

}



bool operator== (T* pT) const {

if (_M_pD == NULL){

return pT == NULL;

}

return *_M_pD == pT;

}



T* get() {

if (_M_pD == NULL) {

return NULL;

}

else {

return _M_pD-> get();

}

}



void release() {

if (_M_pD != NULL) {

--(*_M_pD);

_M_pD = NULL;

}

}



void reset(T* pT) {

if (_M_pD != NULL) {

--(*_M_pD);

_M_pD = NULL;

}

_M_pD = new element(pT);

}



private:

element* get_element()const {

return _M_pD;

}



element* _M_pD;

};

}



#endif


[解决办法]
vc8的实现
// TEMPLATE CLASS auto_ptr
template <class _Ty>
class auto_ptr;

template <class _Ty>
struct auto_ptr_ref
{// proxy reference for auto_ptr copying
auto_ptr_ref(void *_Right)
: _Ref(_Right)
{// construct from generic pointer to auto_ptr ptr
}

void *_Ref;// generic pointer to auto_ptr ptr
};

template <class _Ty>
class auto_ptr
{// wrap an object pointer to ensure destruction
public:
typedef _Ty element_type;

explicit auto_ptr(_Ty *_Ptr = 0) _THROW0()
: _Myptr(_Ptr)
{// construct from object pointer
}

auto_ptr(auto_ptr <_Ty> & _Right) _THROW0()
: _Myptr(_Right.release())
{// construct by assuming pointer from _Right auto_ptr
}

auto_ptr(auto_ptr_ref <_Ty> _Right) _THROW0()
{// construct by assuming pointer from _Right auto_ptr_ref
_Ty **_Pptr = (_Ty **)_Right._Ref;
_Ty *_Ptr = *_Pptr;
*_Pptr = 0;// release old
_Myptr = _Ptr;// reset this
}

template <class _Other>


operator auto_ptr <_Other> () _THROW0()
{// convert to compatible auto_ptr
return (auto_ptr <_Other> (*this));
}

template <class _Other>
operator auto_ptr_ref <_Other> () _THROW0()
{// convert to compatible auto_ptr_ref
_Other *_Testptr = (_Ty *)_Myptr;// test implicit conversion
auto_ptr_ref <_Other> _Ans(&_Myptr);
return (_Testptr != 0 ? _Ans : _Ans);
}

template <class _Other>
auto_ptr <_Ty> & operator=(auto_ptr <_Other> & _Right) _THROW0()
{// assign compatible _Right (assume pointer)
reset(_Right.release());
return (*this);
}

template <class _Other>
auto_ptr(auto_ptr <_Other> & _Right) _THROW0()
: _Myptr(_Right.release())
{// construct by assuming pointer from _Right
}

auto_ptr <_Ty> & operator=(auto_ptr <_Ty> & _Right) _THROW0()
{// assign compatible _Right (assume pointer)
reset(_Right.release());
return (*this);
}

auto_ptr <_Ty> & operator=(auto_ptr_ref <_Ty> _Right) _THROW0()
{// assign compatible _Right._Ref (assume pointer)
_Ty **_Pptr = (_Ty **)_Right._Ref;
_Ty *_Ptr = *_Pptr;
*_Pptr = 0;// release old
reset(_Ptr);// set new
return (*this);
}

~auto_ptr()
{// destroy the object
delete (_Ty *)_Myptr;
}

_Ty& operator*() const _THROW0()
{// return designated value

#if _HAS_ITERATOR_DEBUGGING
if (_Myptr == 0)
_DEBUG_ERROR( "auto_ptr not dereferencable ");
#endif /* _HAS_ITERATOR_DEBUGGING */

__analysis_assume(_Myptr);

return (*(_Ty *)_Myptr);
}

_Ty *operator-> () const _THROW0()
{// return pointer to class object
return (&**this);
}

_Ty *get() const _THROW0()
{// return wrapped pointer
return ((_Ty *)_Myptr);
}

_Ty *release() _THROW0()
{// return wrapped pointer and give up ownership
_Ty *_Tmp = (_Ty *)_Myptr;
_Myptr = 0;
return (_Tmp);
}

void reset(_Ty* _Ptr = 0)
{// destroy designated object and store new pointer
if (_Ptr != _Myptr)
delete (_Ty *)_Myptr;
_Myptr = _Ptr;
}

private:
const _Ty *_Myptr;// the wrapped object pointer
};

热点排行