关于auto_ptr_ref

zcldzh 2011-05-02 06:46:30
auto_ptr(auto_ptr __a) throw() : _M_ptr(__a.release()) { }
上述构造函数不能通过编译。如果能通过编译,就会陷入循环调用.为什么
...全文
73 2 打赏 收藏 转发到动态 举报
写回复
用AI写文章
2 条回复
切换为时间正序
请发表友善的回复…
发表回复
书虫 2011-05-02
  • 打赏
  • 举报
回复
怎么会编译不过!源码都在下面哦!

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
};
zicheng_lin 2011-05-02
  • 打赏
  • 举报
回复
__a.release() 这句应该也会调用a的构造函数,构造函数中调用构造函数导致的

64,682

社区成员

发帖
与我相关
我的任务
社区描述
C++ 语言相关问题讨论,技术干货分享,前沿动态等
c++ 技术论坛(原bbs)
社区管理员
  • C++ 语言社区
  • encoderlee
  • paschen
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
  1. 请不要发布与C++技术无关的贴子
  2. 请不要发布与技术无关的招聘、广告的帖子
  3. 请尽可能的描述清楚你的问题,如果涉及到代码请尽可能的格式化一下

试试用AI创作助手写篇文章吧