auto_ptr_ref的奇妙(下)

 

auto_ptr_ref的奇妙(下)

在我们前面谈到的auto_ptr,它的复制操作的参数类型恰好是非常量引用。所以对于下面的情况它就不能正确处理。

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

auto_ptr<int> fun()//一个生成auto_ptr<int>source函数

{return auto_ptr<int>(new int(8))}

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

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

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

template<class Y>
        operator auto_ptr_ref<Y>() throw() {
            return auto_ptr_ref<Y>(release());

        }

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

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

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

template<class Y> operator auto_ptr_ref<Y>() throw()

{ return auto_ptr_ref<Y>(release());}

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

        auto_ptr(auto_ptr_ref<T> rhs) throw()
         : ap(rhs.yp) {

        }

它的参数类型不是引用,采用的是传值(by value),所以可以接受右值,这时候,调用auto_ptr_ref<int>默认生成的复制构造函数(copy constructor),用上面最后得到的那个auto_ptr_ref<int>临时对象右值作为引数,生成了rhs,接着auto_ptr_ref<int>临时对象右值自动析构结束生命,后面的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<class Y>
    struct auto_ptr_ref {
        Y* yp;
        auto_ptr_ref (Y* rhs)
         : yp(rhs) {
        }
    };

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

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

吴桐写于2003.6.16

最近修改2003.6.16

  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 3
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值