模拟实现智能指针

来源:互联网 发布:里约奥运会直播软件 编辑:程序博客网 时间:2024/06/06 01:34


智能指针:所谓智能指针就是智能/自动化的管理指针所指向的动态资源的释放。

RAII(Resource Acquisition Is Initialization)

资源分配即初始化,定义一个类来封装资源的分配和释放,在构造函数完成资源的分配和初始化,在析构函数完成资源的清理,可以保证资源的正确初始化和释放。

现在来模拟实现一下auto_ptr,scoped_ptr(守卫,即是防拷贝),shared_ptr(使用引用计数)

#include <iostream>using namespace std;//auto_ptrtemplate<typename T>class AutoPtr{public:    AutoPtr(T *ptr):_ptr(ptr){}AutoPtr(AutoPtr<T>& ap):_ptr(ap._ptr){ap._ptr = NULL;}AutoPtr<T>& operator=(AutoPtr<T>& ap){if(_ptr != ap._ptr){delete _ptr;_ptr = ap._ptr;ap._ptr = NULL;}return *this;}T& operator*(){return *_ptr;}T* operator->(){return _ptr;}~AutoPtr(){delete _ptr;}private:T *_ptr;};//scoped_ptrtemplate<typename T>class ScopedPtr{public:    ScopedPtr(T *ptr):_ptr(ptr){}T& operator*(){return *_ptr;}T* operator->(){return _ptr;}~ScopedPtr(){delete _ptr;}protected:ScopedPtr(ScopedPtr<T>& sp);ScopedPtr<T>& operator=(ScopedPtr<T>& sp);private:T *_ptr;};//shared_ptrtemplate<typename T>class SharedPtr{public:    SharedPtr(T *ptr):_ptr(ptr),_pcount(new int(1)){}SharedPtr(SharedPtr<T>& sp):_ptr(sp._ptr),_pcount(sp._pcount){(*_pcount)++;}SharedPtr<T>& operator=(SharedPtr<T>& sp){if(_ptr != sp._ptr){if(--(*_pcount) == 0){delete _pcount;delete _ptr;}_ptr = sp._ptr;_pcount = sp._pcount;(*_pcount)++;}return *this;}T& operator*(){return *_ptr;}T* operator->(){return _ptr;}~SharedPtr(){if(--(*_pcount) == 0){delete _ptr;delete _pcount;}}private:T *_ptr;int *_pcount;};void Test1(){int *p1 = new int(1);int *p2 = new int(2);AutoPtr<int> ap1(p1);AutoPtr<int> ap2(ap1);ap2 = ap1;}void Test2(){int *p1 = new int(1);int *p2 = new int(2);ScopedPtr<int> sp1(p1);//ScopedPtr<int> ap4(ap3);}void Test3(){int *p1 = new int(1);int *p2 = new int(2);SharedPtr<int> sp1(p1);SharedPtr<int> sp2(sp1);SharedPtr<int> sp3(p2);sp3 = sp1;}int main(){//Test1();//Test2();Test3();return 0; }



1 0
原创粉丝点击