auto_ptr<int> ptr1(auto_ptr<int>(new int(1)));
搞懂了上面这句话的执行流程,也就明白了auto_ptr_ref的原理:
执行流程如下:
1:调用explicit auto_ptr(_Ty *_Ptr = 0),完成一个auto_ptr<int>类型的临时变量(为方便表述,将其记为auto_p_tmp)的构造
2:调用template<class _Other> operator auto_ptr_ref<_Other>(),将auto_p_tmp隐式转换为auto_ptr_ref<int>
3:在2中的隐式转换中需要构造一个auto_ptr_ref,所以要调用explicit auto_ptr_ref(_Ty *_Right),完成一个临时的auto_ptr_ref<int>的构造
4:调用auto_ptr(auto_ptr_ref<_Ty> _Right),将生成的auto_ptr_ref<int>的临时变量作为参数,用于构造最终的变量ptr1.
总结一下:
这里的ptr1的构造过程,调用的是template<class _Other> operator auto_ptr_ref<_Other>()这个函数,而没有调用 template<class _Ty> auto_ptr(auto_ptr<_Ty>& _Right) 这一普通的构造函数。