智能指针小结

1. auto_ptr
1> 属于STL的一部分
2>使用时不能使用”operator=”运算符,因为会剥夺所有权。
即:

std::auto_ptr<sample> auto1(new sample);
std::auto_ptr<sample> auto2 = auto1;

此时auto1不能再调用接口。
3>auto_ptr 对象在执行完release函数之后并没有实际的析构。

{
std::auto_ptr<sample> auto1(new sample);
auto1.release();
}

此时sample的析构函数不会被调用到,为了避免此情形,需要:

{
std::auto_ptr<sample> auto1(new sample);
sample * t = auto1.release();
delete t;
}

或者

{
std::auto_ptr<sample> auto1(new sample);
auto1.reset();

}

4>不要作为函数入参传递,及放入容器中等,
(其实就是因为2>,传值过去后,调用处不能再使用这个只能指针。)

  1. boost::scoped_ptr
    1>属于boost的一部分
    待续。。
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值