返回   cpper编程论坛 > 技术杂烩
注册账号 论坛帮助 会员列表 日历事件 搜索 今日新帖 标记版面已读

技术杂烩 找不到地方的技术问题?这里!

回复
 
LinkBack 主题工具 显示模式
  #1 (permalink)  
旧 2004-03-22
高级会员
 
注册日期: 2002-11-22
帖子: 249
clip 正向着好的方向发展
默认 question about auto_ptr

代码:
#include "stdio.h" // auxiliary type to enable copies and assignments (now global) template<class Y> struct auto_ptr_ref { Y* yp; auto_ptr_ref (Y* rhs) : yp(rhs) { printf("auto_ptr_ref::auto_ptr_ref(Y* rhs)\n"); } }; template<class T> class auto_ptr { private: T* ap; // refers to the actual owned object (if any) public: typedef T element_type; // constructor explicit auto_ptr (T* ptr = 0) throw() : ap(ptr) { printf("explicit auto_ptr::auto_ptr (T* ptr)\n"); } // copy constructors (with implicit conversion) // - note: nonconstant parameter auto_ptr (auto_ptr& rhs) throw() : ap(rhs.release()) { printf("auto_ptr::auto_ptr(auto_ptr& rhs)\n"); } template<class Y> auto_ptr (auto_ptr<Y>& rhs) throw() : ap(rhs.release()) { printf("auto_ptr::auto_ptr(auto_ptr<Y>& rhs)\n"); } // assignments (with implicit conversion) // - note: nonconstant parameter auto_ptr& operator= (auto_ptr& rhs) throw() { printf("auto_ptr::operator= (auto_ptr& rhs)\n"); reset(rhs.release()); return *this; } template<class Y> auto_ptr& operator= (auto_ptr<Y>& rhs) throw() { printf("auto_ptr::operator= (auto_ptr<Y>& rhs)\n"); reset(rhs.release()); return *this; } // destructor ~auto_ptr() throw() { printf("auto_ptr::~auto_ptr()\n"); delete ap; } // value access T* get() const throw() { return ap; } T& operator*() const throw() { return *ap; } T* operator->() const throw() { return ap; } // release ownership T* release() throw() { T* tmp(ap); ap = 0; return tmp; } // reset value void reset (T* ptr=0) throw() { if (ap != ptr) { delete ap; ap = ptr; } } /* special conversions with auxiliary type to enable copies and assignments */ auto_ptr(auto_ptr_ref<T> rhs) throw() : ap(rhs.yp) { printf("auto_ptr(auto_ptr_ref<T> rhs)\n"); } auto_ptr& operator= (auto_ptr_ref<T> rhs) throw() { // new printf("auto_ptr& operator= (auto_ptr_ref<T> rhs)\n"); reset(rhs.yp); return *this; } template<class Y> operator auto_ptr_ref<Y>() throw() { printf("auto_ptr::operator auto_ptr_ref<Y>()\n"); return auto_ptr_ref<Y>(release() ); } template<class Y> operator auto_ptr<Y>() throw() { printf("auto_ptr::operator auto_ptr<Y>()\n"); return auto_ptr<Y>(release()); } }; auto_ptr<int> foo1() { auto_ptr<int> p=auto_ptr<int>(new int(1)); return p; // return auto_ptr<int>(new int(1)); } int main() { auto_ptr<int> i(foo1()); }
the code is copied from NicolaiM. Josuttis's book.
I just added couple printf to show myself the execution path.
The running result is
代码:
explicit auto_ptr::auto_ptr (T* ptr) auto_ptr::operator auto_ptr_ref<Y>() auto_ptr_ref::auto_ptr_ref(Y* rhs) auto_ptr(auto_ptr_ref<T> rhs) auto_ptr::~auto_ptr() auto_ptr::operator auto_ptr_ref<Y>() auto_ptr_ref::auto_ptr_ref(Y* rhs) auto_ptr(auto_ptr_ref<T> rhs) auto_ptr::~auto_ptr() auto_ptr::~auto_ptr()
My Question is: why these are two lines of
auto_ptr:perator auto_ptr_ref<Y>()
I can only understand the first call to auto_ptr:perator auto_ptr_ref<Y>(), but not the second one.
Thanks for help!
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
回复时引用此帖
  #2 (permalink)  
旧 2004-03-24
abp 的头像
abp abp 当前离线
高级会员
 
注册日期: 2002-08-30
帖子: 811
abp 正向着好的方向发展
默认

这个问题其实是这样的。首先呢,我觉得这个调用有两个过程,第一是返回,第二是重新传入。
你看到的第二个auto_ptr:perator auto_ptr_ref()就是重新传入的时候打印的。
说实话,我倒觉得第一个auto_ptr:perator auto_ptr_ref()比较混乱。你的两次试验都会有第一个auto_ptr:perator auto_ptr_ref()打印出来,但是其实打印的地方不一样的。
你没注释掉的那次,是在函数里的第一行打印的,那个=其实是一个构造。返回的时候,由于返回的不是rvalue,所以可以直接通过copy ctor走。你在return前面加一个cout<<"clip"<<endl;就会发现正确的途径了。
你注释掉的那次,因为那个值是rvalue,copy ctor走不成了,所以通过auto_ptr_ref转了一下。
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
回复时引用此帖
  #3 (permalink)  
旧 2004-03-24
高级会员
 
注册日期: 2002-11-22
帖子: 249
clip 正向着好的方向发展
默认

懂了,多谢!
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
回复时引用此帖
回复

书签

主题工具
显示模式

发帖规则
不可以发表新主题
不可以发表回复
不可以上传附件
不可以编辑自己的帖子

启用 BB 代码
论坛启用 表情符号
论坛启用 [IMG] 代码
论坛禁用 HTML 代码
Trackbacks are 启用
Pingbacks are 启用
Refbacks are 启用


相似的主题
主题 主题作者 版面 回复 最后发表
[普通]a question about XmlTextReader.ReadBase64() clip 技术杂烩 9 2004-08-29 06:35 AM
question about reference parameter through bind2nd clip 技术杂烩 5 2004-04-26 03:49 AM
Question: C语言里,return能不能返回一个数组? xyz1848 技术杂烩 7 2003-12-13 05:22 PM
May I have a question? Lili yan Zion/测试 3 2003-05-29 02:03 PM
2 question geoffrey 技术杂烩 1 2003-03-17 11:50 AM


所有时间均为格林尼治时间 +9。现在的时间是 10:36 PM


Powered by vBulletin® 版本 3.7.0
版权所有 ©2000 - 2009,Jelsoft Enterprises Ltd.
(C) Copy Right All Right Reserved 2001 - 2007

Search Engine Friendly URLs by vBSEO 3.1.0