一开始还发现这货挺方便的.后来才发现还是不行,
多引用时,就会重复删除了
#include <iostream> #include <memory> using namespace std; void func(const auto_ptr<int>& pInt) { cout << *pInt<<endl; } class X { public: X(){cout <<"construct"<<endl;} ~ X(){cout <<"destructor"<<endl;} }; int main() { #if 0 auto_ptr<int> a(new int(100)); func(a); #endif //error use begin X* x = new X; auto_ptr<X> p1(x); delete x; //destructor 1 //error use begin return 0; } //destructor 2 #if 0 //normal use int main() { cout <<"before scope"<<endl; { auto_ptr<X> p1(new X); cout <<"I will exit the scope" <<endl; } cout <<"after scope"<<endl; cout <<"------------------------------"<<endl; cout <<"before scope"<<endl; { auto_ptr<X> p1(new X); delete p1;; cout <<"I will exit the scope" <<endl; } cout <<"after scope"<<endl; return 0; } #endif //normal use