标签:
1 #include <iostream> 2 #include <memory> 3 #include <vector> 4 using namespace std; 5 6 class A { 7 public: 8 ~A(){ 9 cout<<"delete"<<endl; 10 } 11 }; 12 13 void main() 14 { 15 vector<auto_ptr<A> > tmp; 16 tmp.push_back(auto_ptr<A>(new A())); 17 tmp.push_back(auto_ptr<A>(new A())); 18 }
输出结果:
a delete
a delete
通过输出结果可知,tmp在结束生命周期的时候,智能指针会把其作用指针所指向的内存释放。
1 #include <iostream> 2 #include <memory> 3 #include <vector> 4 using namespace std; 5 6 class A { 7 public: 8 ~A(){ 9 cout<<"delete"<<endl; 10 } 11 }; 12 13 void main() 14 { 15 vector<A*> tmp; 16 tmp.push_back(new A()); 17 delete tmp[0]; 18 }
标签:
原文地址:http://www.cnblogs.com/lewiskyo/p/4604839.html