auto,ptr简单实现,苏大强太强了的博客,auto,ptr实现,香港服务器托管

香港256IP千兆站群服务器BGP专线240元起! 华为云香港物理机精品线路全面上线![特价] 企业级CN2 GIA双程专线高速回国 T3机房 香港美国韩国海外独立物理服务器特价热销中!
/*auto_ptr的实现*/#include<bits/stdc++.h>using namespace std;namespace P{ template <typename T> class Auto_ptr{ public: Auto_ptr(T *da){ s=da; } Auto_ptr(Auto_ptr &da){ s=da.s; da.s=NULL; } ~Auto_ptr(){ if(s!=NULL){ cout<<"调用析构"<<endl; delete(s); } } T& operator*(){ return *s; } T* operator->(){ return s; } protected: T *s; //还可以在这里加一个标记,判断属不属于自己,结果一致。 };}//auto_ptr缺点很明显,管理权交给别人之后,自己指向为空。struct node{ int p;};int main(){ using namespace P; int *da=new int(10); Auto_ptr<node> a(new node{1}); Auto_ptr<node> b(a); return 0;}