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>,传值过去后,调用处不能再使用这个只能指针。)
- boost::scoped_ptr
1>属于boost的一部分
待续。。
本文详细解析了C++智能指针auto_ptr的使用规则,包括其所有权转移特性、析构行为以及避免常见错误的方法。同时,介绍了auto_ptr与其他智能指针如boost::scoped_ptr的区别。

474

被折叠的 条评论
为什么被折叠?



