条款13:以对象管理资源
资源泄漏
void f() {
Investment* pInv = new Investment();
...
delete pInv;
}智能指针
void f() {
std::auto_ptr<Investment> pInv(new Investment());
...
}引用计数型智慧指针
注意智能指针释放资源
Last updated
void f() {
Investment* pInv = new Investment();
...
delete pInv;
}void f() {
std::auto_ptr<Investment> pInv(new Investment());
...
}Last updated
std::auto_ptr<Investment> pInv1(new Investment()); // pInv1 指向资源;
sdt::auto_ptr<Investment> pInv2(pInv1); // pInv2 指向资源,pInv1指向null;
pInv1 = pInv2; // pInv2 指向null,pInv1指向资源;void f() {
std::tr1::shared_ptr<Investment> pInv(new Investment());
...
}