auto_ptr_ref 原理解析

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) 这一普通的构造函数。


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值