auto_ptr_ref干什么用的?

earthharp 2002-11-10 02:11:16
up
...全文
63 25 打赏 收藏 转发到动态 举报
写回复
用AI写文章
25 条回复
切换为时间正序
请发表友善的回复…
发表回复
LoveCreatesBeauty 2003-04-18
  • 打赏
  • 举报
回复
学习
deanjiang 2002-12-06
  • 打赏
  • 举报
回复
flyingbugs(网际)
我手里没有VC6不敢乱讲,不过vc7是有的。我正在写一个模板库,还参考过这些代码呢。
auto_ptr_ref的用途我在前面已经说过了,在强调一句,它不是让你直接使用的,是给编译器做类型转换用的,你的程序永远不需要显示的使用它。
flyingbugs 2002-12-02
  • 打赏
  • 举报
回复
deanjiang(dean) :
vc的stl实现了吗???
vc7我不知道,至少在vc6.0中是没有的!!!

还有,这个auto_ptr_ref基本没什么用.

deanjiang 2002-11-29
  • 打赏
  • 举报
回复
flyingbugs:
vc的stl当然实现了auto_ptr_ref, 就在auto_ptr前面几行的位置。
gcc也实现了。
auto_ptr_ref 的使用方式是一种很标准的方法了,应该是很有名的。
sogald_2001 2002-11-29
  • 打赏
  • 举报
回复
Up.
张友邦 2002-11-29
  • 打赏
  • 举报
回复
我看的是Plauger的auto_ptr实现,其中的operator =会引起被对象释放它所管理的指针,拷贝构造函数不会引起对象的释放。所以,hucong(stupid urchin) 是不是弄错了。
张友邦 2002-11-29
  • 打赏
  • 举报
回复
我也写了个测试程序,
#include <iostream>
#include <memory>

using std::cout;
using std::endl;

class test
{
public:
test() { cout << "ctor" << endl; }
~test() { cout << "dtor" << endl; }
void dosomething() { cout << "do something" << endl; }
};
typedef std::auto_ptr<test> test_ptr;

void fun(test_ptr _obj)
{
_obj->dosomething();
}

void main()
{
test_ptr obj(new test);
fun(obj);
}

输出结果为:

ctor
do something
dtor
flyingbugs 2002-11-29
  • 打赏
  • 举报
回复
template <typename _Tyf>
class smart_ptr
{
public:
typedef _Tyf element_type;
smart_ptr(_Tyf * _P=0) throw()
:_Owns(_P!=0),_Ptr(_P){}
smart_ptr(const smart_ptr<_Tyf> &_Y) throw()
:_Owns(_Y._Owns),_Ptr(_Y.release()){}
smart_ptr<_Tyf>& operator=(const smart_ptr<_Tyf>& _Y) throw()
{
if(this != &_Y)
{
if(_Owns)
{
if(_Ptr != _Y._Ptr)
{
delete _Ptr;
_Owns = _Y._Owns;
_Ptr = _Y.release();
}
else
{
_Owns = _Owns||_Y._Owns;
}

}
else
{
_Owns = _Y._Owns;
_Ptr = _Y.release();
}
}
return (*this);
}
~smart_ptr()
{
cout<< "before delete" << endl;
cout<< "_Owns: "<< _Owns << endl;
if (_Owns)
delete _Ptr;
cout << "~smart_ptr()" << endl;
}
_Tyf& operator*() const throw()
{return (*get()); }
_Tyf *operator->() const throw()
{return (get()); }
_Tyf *get() const throw()
{return (_Ptr); }
_Tyf *release() const throw()
{((smart_ptr<_Tyf> *)this)->_Owns = false;
return (_Ptr); }

private:
bool _Owns;
_Tyf* _Ptr;
};
flyingbugs 2002-11-29
  • 打赏
  • 举报
回复
auto_ptr_ref
一般都没有实现

你可以看看vc带的stl的源代码。
deanjiang 2002-11-26
  • 打赏
  • 举报
回复
其实事情很简单,怎么会有这么多人不清楚。
剧个例子
auto_ptr<int> foo(auto_ptr<int>);
我可以这样调用
auto_ptr<int> ptrInt=foo(new int);
参数中的指针会自动构造一个auto_ptr<int>对象,是一个临时变量变量,也就是受是一个右值。auto_ptr的拷贝构造函数使用非const的引用,需要左值进行结合,为了参数匹配通过auto_ptr_ref进行了两次类型转换,从而逃过了对右值的约束。函数的返回值的处理也是类似的。
根本上,auto_ptr的拷贝函数的语义并不是拷贝,这才是引出麻烦的根源
hucong 2002-11-15
  • 打赏
  • 举报
回复
class A
{
public:
A(){std::cout<<"A()\n";}
~A(){std::cout<<"~A()\n";}
const char* getClassName()const{return "A";}
};

void f(std::auto_ptr_ref<A> aRef)
{//如果把auto_ptr_ref改成auto_ptr看看,结果会输出几个构造和析构
aRef._Ref->getClassName();
}

int main(int argc, char* argv[])
{
using namespace std;
auto_ptr<A> a(new A);
f(a);

return 0;
}

这是我写测试类,不知道对你们的问题有没有帮助,建议大家看看boost库四个灵巧指针的实现,那里根本就不需要这么麻烦了,而且有两个能用户STL标准容器.
kxw 2002-11-14
  • 打赏
  • 举报
回复
7up
kxw 2002-11-13
  • 打赏
  • 举报
回复
我也想请教,up

不过,我写的可是源码里的注释!
earthharp 2002-11-13
  • 打赏
  • 举报
回复
up
earthharp 2002-11-12
  • 打赏
  • 举报
回复
up
up
zhuoshenme 2002-11-11
  • 打赏
  • 举报
回复
to earthharp
看书的时候,我也不太清楚他的用法,为了让自己能再过一遍,就摘抄过来了
刚刚也试了一下,也没有弄清楚

看来,要请斑竹了
earthharp 2002-11-11
  • 打赏
  • 举报
回复
难道这也能算理由吗?
哪些不支持的language features是什么呢?
kxw 2002-11-11
  • 打赏
  • 举报
回复
别人有什么观点吗??

up
kxw 2002-11-11
  • 打赏
  • 举报
回复
要问为什么,这就是为什么
According to the C++ standard, these conversions are required. Most present-day compilers, however, do not enforce that requirement---and, in fact, most present-day compilers do not support the language features that these conversions rely on.
kxw 2002-11-11
  • 打赏
  • 举报
回复
源码中的确不是const


auto_ptr(auto_ptr& __a) __STL_NOTHROW : _M_ptr(__a.release()) {}
加载更多回复(5)

24,854

社区成员

发帖
与我相关
我的任务
社区描述
C/C++ 工具平台和程序库
社区管理员
  • 工具平台和程序库社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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