int main() {
std::auto_ptr<int> p;
std::auto_ptr<int> p2;
p = std::auto_ptr<int>(new int);
*p = 11;
p2 = p;
// std::cout << "p points to " << *p << '\n'; //p is now null-pointer auto_ptr
std::cout << "p2 points to " << *p2 << '\n'; //此时输出的是11
p2.reset(new int); //上个new 被delete
std::cout << "p2 points to " << *p2 << '\n'; //此时输出 0
int * p3=p2.release();
// std::cout << "p2 points to " << *p2 << '\n'; //p2 不可用了
cout << "p3 points to " << *p3 << '\n'; //输出0
return 0;
}
p = std::auto_ptr<int>(new int);
参数new 出来的指针没通过delete释放。
用于基本类型
需引用#include <memory>>