auto_ptr_ref的奇妙(下) (转)

auto_ptr_ref的奇妙(下) (转)[@more@] 

auto_ptr_ref的奇妙(下)

在我们前面谈到的auto_ptr,它的复制操作的参数类型恰好是非常量引用。所以对于下面的情况它就不能正确处理。XML:namespace prefix = o ns = "urn:schemas-microsoft-com:Office:office" />

auto_ptr ap1 = auto_ptr(new int(8));//等号右边的是一个临时右值

auto_ptr fun()//一个生成auto_ptr的source函数

{return auto_ptr(new int(8))}

auto_ptr ap2 ( fun() );//调用fun()生成的auto_ptr是右值

而这种情况不但合法,也是很常见的,我们不能拒绝这种用法,怎么办?天才的C++库设计者已经为我们想到了这一点,auto_ptr_ref的引入就是为了解决这个问题。仔细观察最下面的auto_ptr实现代码,我们会看到这样几行:

 /* special conversions with auxiliary type to enable copies and assignments */
 auto_ptr(auto_ptr_ref rhs) throw()
 : ap(rhs.yp) {
 }
 auto_ptr& operator= (auto_ptr_ref rhs) throw() { // new
 reset(rhs.yp);
 return *this;
 }

template
 operator auto_ptr_ref() throw() {
 return auto_ptr_ref(release());
 }

这就是关键所在了。对于一个auto_ptr右值,不可能为它调用正常的赋值运算符函数和复制构造函数,举个例子说明,对于语句

auto_ptr ap1 = auto_ptr(new int(8));

首先生成临时对象右值auto_ptr(new int(8)),然后使用转型函数模板

template operator auto_ptr_ref() throw()

{ return auto_ptr_ref(release());};

由auto_ptr(new int(8)),首先调用成员函数release(),然后由获取的原始指针生成另外一个临时对象右值auto_ptr_ref(一个指向动态存储区中8的指针)。这个时候我们再看一个构造函数

 auto_ptr(auto_ptr_ref rhs) throw()
 : ap(rhs.yp) {
 }

它的参数类型不是引用,采用的是传值(by value),所以可以接受右值,这时候,调用auto_ptr_ref默认生成的复制构造函数(copy constructor),用上面最后得到的那个auto_ptr_ref临时对象右值作为引数,生成了rhs,接着auto_ptr_ref临时对象右值自动析构结束生命,后面的ap(rhs.yp)完成最后的工作。

我们的整个探险过程就结束了。最好参考《The C++ Standard Library》中讲解auto_ptr使用的章节一起阅读。这样auto_ptr的使用和所有设计原理对我们就不再神秘了。

附录:auto_ptr的实现代码,来自Nicolai M. Josuttis

/* class auto_ptr
 * - improved standard confoRming implementation
 */
namespace std {
 // auxiliary type to enable copies and assignments (now global)
 template
 struct auto_ptr_ref {
 Y* yp;
 auto_ptr_ref (Y* rhs)
 : yp(rhs) {
 }
 };

 template
 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) {
 }

 // copy constructors (with implicit conversion)
 // - note: nonconstant parameter
 auto_ptr (auto_ptr& rhs) throw()
 : ap(rhs.release()) {
 }
 template
 auto_ptr (auto_ptr& rhs) throw()
 : ap(rhs.release()) {
 }
 
 // assignments (with implicit conversion)
 // - note: nonconstant parameter
 auto_ptr& operator= (auto_ptr& rhs) throw() {
 reset(rhs.release());
 return *this;
 }
 template
 auto_ptr& operator= (auto_ptr& rhs) throw() {
 reset(rhs.release());
 return *this;
 }
 
 // destructor
 ~auto_ptr() throw() {
 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 rhs) throw()
 : ap(rhs.yp) {
 }
 auto_ptr& operator= (auto_ptr_ref rhs) throw() { // new
 reset(rhs.yp);
 return *this;
 }
 template
 operator auto_ptr_ref() throw() {
 return auto_ptr_ref(release());
 }
 template
 operator auto_ptr() throw() {
 return auto_ptr(release());
 }
 };
}

吴桐写于2003.6.16

最近修改2003.6.16


来自 “ ITPUB博客 ” ,链接:http://blog.itpub.net/10752019/viewspace-959304/,如需转载,请注明出处,否则将追究法律责任。

转载于:http://blog.itpub.net/10752019/viewspace-959304/

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值