const auto_ptr保证拥有权不能转移的实现原理?

const auto_ptr保证拥有权不能转移的实现原理?
const auto_ptr保证拥有权不能转移的实现原理?

在《C++标准程序库》p55,提到了auto_ptr使用了一个技巧,能够copy和复制non-const auto_ptr,但不可以copy和复制const atuo_ptr。

可看了之后没感觉,等待高手指点中.....
CSDN领分之处:
http://community.csdn.net/Expert/topic/4347/4347660.xml?temp=.3555872

VC7的
//VC7中的



    
// 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(auto_ptr<_Ty>& _Right)
        : _Ref(_Right)
        
{    // construct from compatible auto_ptr
        }


    auto_ptr
<_Ty>& _Ref;    // reference to constructor argument
    }
;

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()
        : _Myptr(_Right._Ref.release())
        
{    // construct by assuming pointer from _Right auto_ptr_ref
        }


    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
        return (auto_ptr_ref<_Other>(*this));
        }


    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)
        reset(_Right._Ref.release());
        
return (*this);
        }


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


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


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


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


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


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


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

你可能感兴趣的:(const auto_ptr保证拥有权不能转移的实现原理?)