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的一部分
待续。。