/*
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;
}
auto_ptr简单实现
猜你喜欢
转载自blog.csdn.net/weixin_41033366/article/details/105238671
今日推荐
周排行