//#include<iostream>
//using namespace std;
//模擬auto_ptr(不要使用)
/*AutoPtr只是實現了構造函數和析構函數,對於拷貝構造函數和賦值運算符重載是系統默認的,
系統默認情況下是值拷貝,存在一塊空間被釋放兩次及以上的情況,導致程序運行錯誤。*/
//template<typename T>
//class Auto_ptr
//{
//public:
// Auto_ptr( T* p)
// :_p(p)
// {
// }
// ~Auto_ptr()
// {
// if(_p!=NULL)
// {
// delete _p;
// _p=NULL;
// }
// }
//public:
// T& operator *()
// {
// return *_p;
// }
// T& operator ->()
// {
// return _p;
// }
//
//private:
// T* _p;
//};
//void FunTest()
//{
// Auto_ptr<int>p1= new int;
// Auto_ptr<int>p2(p1);
// *p1=10;
//}
//int main()
//{
// FunTest();
// getchar();
// return 0;
//}
//解決Auto_Ptr的方法,將第一個指向這塊空間的指針直接置爲空,然後進行析構。
//template<typename T>
//class Auto_ptr
//{
//public:
// Auto_ptr( T* p)
// :_p(p)
// {
// }
// ~Auto_ptr()
// {
// if(_p!=NULL)//一定要判斷是否爲NULL;
// {
// delete _p;
// _p=NULL;
// }
// }
// Auto_ptr(Auto_ptr &p)
// :_p(p._p)
// {
// p._p=NULL;
// }
// Auto_ptr& operator =(Auto_ptr& p)
// {
// if(this!=&p)
// {
// delete _p;
// _p=p._p;
// p._p=NULL;
// }
// return *this;
// }
//public:
// T& operator *()
// {
// return *_p;
// }
// T& operator ->()
// {
// return _p;
// }
//
//private:
// T* _p;
//};
//void FunTest()
//{
// Auto_ptr<int>p1= new int;
// *p1=10;
// Auto_ptr<int>p2(p1);
// cout<<*p2<<endl;
//}
//int main()
//{
// FunTest();
// getchar();
// return 0;
//}
//模擬 ScopedPtr
/*ScopedPtr就是隻要防止創建的指針對象進行復制和拷貝構造;
我們只要在類裏面將它的拷貝構造和賦值運算符給成protected就行,只給聲明,不給定義,
當用戶進行賦值和拷貝構造時候,編譯階段就會報錯*/
//template<typename T>
//class ScopedPtr
//{
//public:
// ScopedPtr( T* p)
// :_p(p)
// {
// }
// ~ScopedPtr()
// {
// if(_p!=NULL)
// {
// delete _p;
// _p=NULL;
// }
// }
//public:
// T& operator *()
// {
// return *_p;
// }
// T& operator ->()
// {
// return _p;
// }
//protected:
// //防止拷貝
// ScopedPtr(ScopedPtr&);
// ScopedPtr& operator =(ScopedPtr&);
//private:
// T* _p;
//};
//void FunTest()
//{
// ScopedPtr<int>p1=new int;
// *p1=10;
//}
//int main()
//{
// FunTest();
// getchar();
// return 0;
//}
// 模擬ScopedArray
//template<typename T>
//class ScopedArray
//{
//public:
// ScopedArray( T*p=NULL )
// :_p(p)
// {
// }
// ~ScopedArray()
// {
// if(_p!=NULL)
// {
// delete[] _p;
// _p=NULL;
// }
// }
//public:
// T& operator *()
// {
// return *_p;
// }
// T& operator ->()
// {
// return _p;
// }
// T& operator [](size_t index)
// {
// assert(index>0);
// return _p[index];
// }
// const T& operator [](size_t index)const
// {
// assert(index>0);
// return _p[index];
// }
//
//protected:
// //防止拷貝
// ScopedArray(ScopedArray&);
// ScopedArray& operator =(ScopedArray&);
//private:
// T* _p;
//};
//void FunTest()
//{
// ScopedArray<int>p1=new int[100];
// int a[10]={1,23,4,5,6,7,8,9,7,9};
// *p1=a[0];
//}
//int main()
//{
// FunTest();
// getchar();
// return 0;
//}
//模擬實現 SharedPtr
/*SharedPtr指針主要的原理是利用引用計數的淺拷貝來實現,通過多開闢4個字節的方式,存儲引用計數,當有指針指向這塊空間時,引用計數+1。如若析構時,
先將這塊空間的引用計數降爲1,然後在進行析構,避免了析構多次的問題*/
//template<typename T>
//class SharedPtr
//{
//public:
// SharedPtr( T* p=NULL)
// :_p(p)
// ,_pcount(new int(1))
// {}
// SharedPtr(SharedPtr&p)
// :_p(p._p)
// ,_pcount(p._pcount )
// {
// ++(*_pcount);
// }
// SharedPtr&operator =(SharedPtr &p)
// {
// if(NULL==_p)//自己管理一塊空間
// {
// _p=p._p;
// _pcount=p._pcount;
// ++(*_pcount);
// }
// if(1==*_pcount&&_p!=p._p)//自己獨佔一塊空間
// {
// delete _p;
// delete _pcount;
// _p=p._p;
// _pcount=p._pcount;
// ++(*_pcount);
// }
// if((*_pcount)>1)//和別人共用一塊空間
// {
// --(*_pcount);
// _p=p._p;
// _pcount=p._pcount;
// ++(*_pcount);
// }
// return *this;
// }
// ~SharedPtr()
// {
// (*_pcount)--;
// if((*_pcount)==0)
// {
// delete _p;
// delete _pcount;
// _p=NULL;
// _pcount=NULL;
// }
// }
//public:
// T& operator *()
// {
// return *_p;
// }
// T& operator ->()
// {
// return _p;
// }
// int *_pcount;
//private:
// T* _p;
//
//};
//void FunTest()
//{
// SharedPtr<int>p1= new int;
// SharedPtr<int>p2(p1);
// SharedPtr<int>p3=new int;
// p2=p3;
// SharedPtr<int>p4=new int;
// p2=p4;
// cout<<*(p1._pcount)<<endl;
// cout<<*(p2._pcount)<<endl;
// cout<<*(p3._pcount)<<endl;
// /*cout<<*(p4._pcount)<<endl;*/
//
//}
//int main()
//{
// FunTest();
// getchar();
// return 0;
//}
智能指針的實現
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.