智能指针是用模板实现,创建对象后,对象管理了资源,然后通过析构函数释放掉该资源,以防内存泄漏。
auto_ptr(空间管理权限的转移,永远只有一个指针指向动态开辟的空间)
#include <iostream>
using namespace std;
template<class T>
class AutoPtr
{
public:
AutoPtr(T* p = NULL)
:_p(p)
{
cout<<"AutoPtr()"<<endl;
}
AutoPtr(AutoPtr<T>& ap)
:_p(ap._p)
{
ap._p = NULL;
}
AutoPtr<T>& operator =(AutoPtr<T>& ap)
{
if(this != &ap)
{
if(_p != NULL)
delete _p;
_p = ap._p;
ap._p = NULL;
}
return *this;
}
~AutoPtr()
{
cout<<"~AutoPtr()"<<endl;
if(_p)
delete _p;
}
T& operator *()
{
return *_p;
}
T* operator ->()
{
return _p;
}
private:
T* _p;
};
struct A
{
int a;
int b;
};
int main()
{
AutoPtr<int> ap1(new int(1));
AutoPtr<int> ap2(ap1);
AutoPtr<int> ap3(new int(2));
ap3 = ap2;
cout<<*ap3<<endl;
AutoPtr<A> ap4(new A);
ap4->a = 1;
ap4->b = 2;
cout<<ap4->a<<endl;
cout<<ap4->b<<endl;
return 0;
}
auto_ptr(用_owner来标记空间的释放权,防止重复释放同一块空间)
#include <iostream>
using namespace std;
template<class T>
class AutoPtr
{
public:
AutoPtr(T* p = NULL)
:_p(p)
,_owner(true)
{
if(_owner == NULL)
_owner = false;
}
AutoPtr(AutoPtr<T>& ap)
:_p(ap._p)
,_owner(true)
{
ap._owner = false;
}
AutoPtr<T>& operator =(AutoPtr<T>& ap)
{
if(this != &ap)
{
if(_p)
delete _p;
_p = ap._p;
_owner = true;
ap._owner = false;
}
return *this;
}
~AutoPtr()
{
if(_owner)
{
delete _p;
_p = NULL;
_owner = false;
}
}
T& operator *()
{
return *_p;
}
T* operator ->()
{
return _p;
}
private:
T* _p;
mutable bool _owner;
};
struct A
{
int a;
int b;
};
int main()
{
AutoPtr<int> ap1(new int(1));
AutoPtr<int> ap2(ap1);
AutoPtr<int> ap3(new int(2));
ap3 = ap2;
cout<<*ap1<<endl;
cout<<*ap2<<endl;
cout<<*ap3<<endl;
AutoPtr<A> ap4(new A);
ap4->a = 10;
ap4->b = 20;
cout<<ap4->a<<endl;
cout<<ap4->b<<endl;
return 0;
}
scoped_ptr(防止拷贝,防止赋值)
#include <iostream>
using namespace std;
template<class T>
class ScopedPtr
{
public:
friend void Fun();
ScopedPtr(T* p = NULL)
{}
T& operator *()
{
return *_p;
}
~ScopedPtr()
{
if(_p)
{
delete _p;
_p = NULL;
}
}
private:
ScopedPtr(ScopedPtr<T>& sp);
ScopedPtr<T>& operator =(const ScopedPtr<T>& sp);
T* _p;
};
void Fun()
{
ScopedPtr<int> sp1(new int);
ScopedPtr<int> sp2;
}
int main()
{
Fun();
ScopedPtr<int> sp1(new int(1));
ScopedPtr<int> sp2;
return 0;
}
scoped_array管理数组
#include <iostream>
using namespace std;
template<class T>
class ScopedArray
{
public:
ScopedArray(T* p = NULL)
:_p(p)
{}
~ScopedArray()
{
if(_p)
{
delete _p;
_p = NULL;
}
}
T& operator [](int index)
{
return _p[index];
}
const T& operator [](int index)const
{
return _p[index];
}
private:
ScopedArray(ScopedArray<T>& sp);
ScopedArray<T>& operator =(const ScopedArray<T>& sp);
T* _p;
};
int main()
{
ScopedArray<int> sp1(new int[10]);
sp1[0] = 0;
sp1[1] = 1;
return 0;
}
shared_ptr(通过引用计数实现对空间的管理)
#include <iostream>
using namespace std;
template<class T>
class SharedPtr
{
public:
SharedPtr(T* p =NULL)
:_p(p)
,_pcount(NULL)
{
if(_p)
_pcount = new int(1);
}
SharedPtr(const SharedPtr<T>& sp)
:_p(sp._p)
,_pcount(sp._pcount)
{
(*sp._pcount)++;
}
SharedPtr<T>& operator =(const SharedPtr<T>& sp)
{
if(this != &sp)
{
Release();
_p = sp._p;
_pcount = sp._pcount;
(*sp._pcount)++;
}
return *this;
}
~SharedPtr()
{
Release();
}
T& operator *()
{
return *_p;
}
T* operator ->()
{
return _p;
}
private:
void Release()
{
if(_p && (--(*_pcount)) == 0)
{
delete _p;
_p = NULL;
delete _pcount;
_pcount = NULL;
}
}
T* _p;
int* _pcount;
};
int main()
{
SharedPtr<int> sp1(new int(1));
SharedPtr<int> sp2(sp1);
SharedPtr<int> sp3(new int(3));
sp3 = sp1;
return 0;
}
shared_array管理数组
#include <iostream>
using namespace std;
template<class T>
class SharedArray
{
public:
SharedArray(T* p =NULL)
:_p(p)
,_pcount(NULL)
{
if(_p)
_pcount = new int(1);
}
SharedArray(const SharedArray<T>& sp)
:_p(sp._p)
,_pcount(sp._pcount)
{
(*sp._pcount)++;
}
SharedArray<T>& operator =(const SharedArray<T>& sp)
{
if(this != &sp)
{
Release();
_p = sp._p;
_pcount = sp._pcount;
(*sp._pcount)++;
}
return *this;
}
~SharedArray()
{
Release();
}
T& operator *()
{
return *_p;
}
T* operator ->()
{
return _p;
}
T& operator [](size_t index)
{
return _p[index];
}
const T& operator [](size_t index)const
{
return _p[index];
}
private:
void Release()
{
if(_p && (--(*_pcount)) == 0)
{
delete _p;
_p = NULL;
delete _pcount;
_pcount = NULL;
}
}
T* _p;
int* _pcount;
};
int main()
{
SharedArray<int> sp(new int[10]);
sp[0] = 0;
sp[1] = 1;
sp[2] = 2;
return 0;
}