## auto_ptr 如何实现

renzhewh 2010-12-05 09:33:28

模板类 auto_ptr 给出了一种智能指针形式。
这里我想知道关于其辅助类 auto_ptr_ref 的作用?

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

_Ty *_Ref; // generic pointer to auto_ptr ptr
};
【VS2008】


1、下面是我从《C++ STL》中文版 第四章 摘录的部分解释(供参考):
考虑那些成员模板函数时,事情变得复杂了,在对象所存储的指针可以互换或者赋值的前提下,它们
有意地允许在这些对象间进行互换或者赋值。它们同样也有意地允许将这样的对象作为参数传递给函
数调用以及作为函数的返回值使用。

这就是添加嵌套类auto_ptr_ref以及那个有趣的构造函数和模板类型转换操作符来处理该嵌套类对象
的原因。

【注意这里说的是‘嵌套类’,可是VS2008里的实现却不是这样,为什么?】

这种方法的理论基础依赖于C++标准中一个相当深奥的话题【指的是什么?】

...全文
110 9 打赏 收藏 转发到动态 举报
写回复
用AI写文章
9 条回复
切换为时间正序
请发表友善的回复…
发表回复
renzhewh 2010-12-07
  • 打赏
  • 举报
回复
还想问下,新的 C++ 标准有了 move constructor 之后,auto_ptr_ref 是否就不需要了
renzhewh 2010-12-05
  • 打赏
  • 举报
回复
o,你说的对,我想错了我
qq120848369 2010-12-05
  • 打赏
  • 举报
回复
我不清楚你什么意思,解释的很详细了,我说的不好理解就去看一下链接行了.
renzhewh 2010-12-05
  • 打赏
  • 举报
回复
顺便提下 auto_ptr<int*> --> auto_ptr<int>
renzhewh 2010-12-05
  • 打赏
  • 举报
回复
谢谢

不过 2、3 好像不完全对啊

【4,当我们auto_ptr<int*> left=auto_ptr<int*>(new int(2));时,=右边就是一个临时对象,
此时不能调用形参为auto_ptr &的构造函数来构造left,所以只能调用auto_ptr_ref形参的构造
函数,所以要给auto_ptr增加一个类型转化operator auto_ptr_ref().】

这个似乎并不能成为使用 auto_ptr_ref 的理由,此时调用构造函数是 auto_ptr(T* ptr),无论
使用 auto_ptr_ref 与否。

此外 const auto_ptr 的意思应该是不转让拥有权,但你的实现却允许了,这似乎已经违背了auto_ptr
应有的语义。

最后这似乎都不是我要的答案

这里最想知道:有什么是 【More Effective C++ 中实现版本】 做不到的?
qq120848369 2010-12-05
  • 打赏
  • 举报
回复
#include <iostream>
using namespace std;

class Auto_ptr
{
public:
Auto_ptr(int *p):point(p)
{
}

Auto_ptr(const Auto_ptr &obj):point(obj.release())
{
}

~Auto_ptr()
{
delete point;
}

int* release() const
{
int *p=point;
point=NULL;

return p;
}

int& operator* ()
{
return *point;
}

private:
mutable int *point;
};

int main()
{
Auto_ptr a=Auto_ptr(new int(3));
Auto_ptr b(new int(4));

cout<<*a<<*b<<endl;

return 0;
}
qq120848369 2010-12-05
  • 打赏
  • 举报
回复
这应该是一种设计完美的考虑而已,简单的利用C++语言,只写一个const auto_ptr &的构造函数,并且给auto_ptr类增加一个const函数_Ty* release()const{_Ty *p=this->point; this->point=0;return p;},然后mutable _Ty *point;也可以实现这个功能. 无论左值还是右值,一律用const auto_ptr &obj接受,调用obj.release()得到指针,并且还可以修改const对象的成员,我会写一段代码示例.
qq120848369 2010-12-05
  • 打赏
  • 举报
回复
http://www.javaeye.com/topic/746062


我仔细读了一下,感觉思路清晰了,以下是我的讲解,帮助楼主理解

1,auto_ptr类保证同一个指针,同一时刻只允许一个auto_ptr保持它,所以拷贝与赋值都会让实参的auto_ptr失去控制权,所以不能使用const auto_ptr & 这样的形参.

2,不能使用const形参,则必须使用auto_ptr &,但是当传入的实参是右值时,即一个临时auto_ptr对象时,又必须使用const auto_ptr &.

3,所以,我们定义auto_ptr &的形参的拷贝构造函数来接受左值auto_ptr实参,定义一个中间类auto_ptr_ref来解决传入auto_ptr对象为右值的情况,增加一个构造函数形参为auto_ptr_ref.

4,当我们auto_ptr<int*> left=auto_ptr<int*>(new int(2));时,=右边就是一个临时对象,此时不能调用形参为auto_ptr &的构造函数来构造left,所以只能调用auto_ptr_ref形参的构造函数,所以要给auto_ptr增加一个类型转化operator auto_ptr_ref().

5,在auto_ptr_ref里,右值auto_ptr必须让自己的指针为NULL,并且将指针的所有权给auto_ptr_ref对象管理,转化完成后,右值auto_ptr已经是个废物,随便它自生自灭.

6,此时,我们得到了一个auto_ptr_ref临时对象,里边装有new int(2)这个指针,由于我们的auto_ptr有接受auto_ptr_ref类型的构造函数,所以成功匹配,将auto_ptr_ref里的指针赋值给=左边的auto_ptr对象,而我们只要保证auto_ptr_ref的拷贝函数只是复制指针,auto_ptr_ref的析构函数什么都不做就可以了.
renzhewh 2010-12-05
  • 打赏
  • 举报
回复

2、这里是从《The C++ Programming Language》14.4.2 中摘录
auto_ptr_ref的用途就是为普通auto_ptr实现破坏性复制语义,这也使const auto_ptr不可能赋值。

【不懂;不使用它,也可以让const 不能赋值啊,这可以参阅More Effective c++ 中的实现。此外在
该书中,同样将auto_ptr_ref作为嵌套类】

3、VS2008中提供的源码
这里的实现有这样一个方法,_Right是传值,在方法内将其数据改变,
【好像没用啊?】

auto_ptr(auto_ptr_ref<_Ty> _Right) _THROW0()
{ // construct by assuming pointer from _Right auto_ptr_ref
_Ty *_Ptr = _Right._Ref;
_Right._Ref = 0; // release old 【注意】
_Myptr = _Ptr; // reset this
}
类似的还有
auto_ptr<_Ty>& operator=(auto_ptr_ref<_Ty> _Right) _THROW0()
{ // assign compatible _Right._Ref (assume pointer)
_Ty *_Ptr = _Right._Ref;
_Right._Ref = 0; // release old【注意】
reset(_Ptr); // set new
return (*this);
}

64,654

社区成员

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

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