求教auto_ptr源码中模板形参的问题

wokonglinglude 2011-07-18 03:22:59
各位大虾们好,在看到auto_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()
: _Myptr(_Right._Ref.release())
{ // construct by assuming pointer from _Right auto_ptr_ref
}

//?????????????????????????????????????????????????????????????????????????
//此处class_other是何解,好奇怪的语法,貌似是个构造函数。求解此处的详细剖析。
//模板类里面再次出现模板用处,还有operator 一个类型不明白
//??????????????????????????????????????????????????????????????????????????
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
};
...全文
103 11 打赏 收藏 转发到动态 举报
写回复
用AI写文章
11 条回复
切换为时间正序
请发表友善的回复…
发表回复
pathuang68 2011-07-18
  • 打赏
  • 举报
回复
看C++ Primer,里面对auto_ptr解释得很透彻。
pengzhixi 2011-07-18
  • 打赏
  • 举报
回复
template<class _Other>
operator auto_ptr<_Other>()当你这个_Other 是一个派生类类型的时候你就知道用处了。

wokonglinglude 2011-07-18
  • 打赏
  • 举报
回复
[Quote=引用 8 楼 maoxing63570 的回复:]
C/C++ code
cout<<test*b<<endl;
//细节 何解???
[/Quote]
能告诉我语法和怎么调用的吗?
maoxing63570 2011-07-18
  • 打赏
  • 举报
回复

#include "stdafx.h"
#include <iostream>
using namespace std;

class Test
{
public:
Test()
{
a=1;
}
operator int()const
{
return a;
}
private:
int a;
};

int _tmain(int argc, _TCHAR* argv[])
{
int b=3;
Test test;
cout<<test*b<<endl;
system("pause");
return 0;
}

这东西,翻翻书,很容易懂的
Vegertar 2011-07-18
  • 打赏
  • 举报
回复
你的这个源码注释太简陋了。

直接看你的编译器里的,注释详细的多。
maoxing63570 2011-07-18
  • 打赏
  • 举报
回复
#include "stdafx.h"
#include <iostream>
using namespace std;

class Test
{
public:
Test()
{
a=1;
}
operator int()const
{
return a;
}
private:
int a;
};

int _tmain(int argc, _TCHAR* argv[])
{
int b=3;
Test test;
cout<<test*b<<endl;
system("pause");
return 0;
}
Vegertar 2011-07-18
  • 打赏
  • 举报
回复
抱歉,看错了。

成员模板,是为了提供兼容指针类型的转换。也就是在同一个类继承层次中。比如子类指针向父类的casting
wokonglinglude 2011-07-18
  • 打赏
  • 举报
回复
[Quote=引用 2 楼 dizuo 的回复:]
template<class _Other>
operator auto_ptr<_Other>() _THROW0()
{ // convert to compatible auto_ptr 转换成兼容的auto_ptr return (auto_ptr<_Other>(*this));
}
=======
模板 隐式转化符
[/Quote]
再详细一点吧。怎么转换,没碰到过这种语法啊。
Vegertar 2011-07-18
  • 打赏
  • 举报
回复
因为使用了非常量引用的复制构造函数来作所有权转让,所以引入一个额外的中间类型作中间转换。
源码里的注释写的很清楚了。


/**
* @brief Automatic conversions
*
* These operations convert an %auto_ptr into and from an auto_ptr_ref
* automatically as needed. This allows constructs such as
* @code
* auto_ptr<Derived> func_returning_auto_ptr(.....);
* ...
* auto_ptr<Base> ptr = func_returning_auto_ptr(.....);
* @endcode
*/
auto_ptr(auto_ptr_ref<element_type> __ref) throw()
: _M_ptr(__ref._M_ptr) { }

ryfdizuo 2011-07-18
  • 打赏
  • 举报
回复
template<class _Other>
operator auto_ptr<_Other>() _THROW0()
{ // convert to compatible auto_ptr
return (auto_ptr<_Other>(*this));
}
=======
模板 隐式转化符
未注销 2011-07-18
  • 打赏
  • 举报
回复
_Other不就是一个型别么?

64,640

社区成员

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

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