C++?е????????
???????????? ???????[ 2017/3/8 11:06:54 ] ??????????? C++
??????????????????????????????????????????????????????????????????????????????????????????????????????
????void fun()
????{
????int *ptr=new int(1);
????if(1)
????throw 1;
????delete ptr;
????}
????void test1()
????{
????try
????{
????fun();
????}
????catch(...)
????{
????cout<<"δ???"<<endl; pre="">
????????????????????????????????г????????????????????????й???????(ptr??????)???????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????.????????????????????????????-RAII(Resource Acquisition Initialization)????????估?????????????????????????????????
????auto_ptr????????
????template<typename t="">
????class AutoPtr
????{
????public:
????AutoPtr(T *ptr)
????:_ptr(ptr)
????{
????cout<<"AutoPtr()"<<endl; t="">& ap) //??????
????{
????_ptr=ap._ptr;
????ap._ptr=0;
????}
????AutoPtr<t>& operator=(AutoPtr<t>& ap)
????{
????if(_ptr != ap._ptr)
????{
????delete _ptr;
????_ptr=ap._ptr;
????ap._ptr=0;
????}
????return *this;
????}
????T& operator*()
????{
????return *_ptr;
????}
????T* operator->()
????{
????return _ptr;
????}
????void Reset(T *ptr) //????
????{
????delete _ptr;
????_ptr=ptr;
????}
????~AutoPtr()
????{
????if(_ptr != 0)
????{
????cout<<"~AutoPtr()"<<endl; _ptr="0;" bool="" delete="" pre="" private:="">
????auto_ptr????????????????????????????????????????????ε??????.??ν???????????????????????????????????????????????????????????????е???????????????????????????????????????????????????????????????????????????????????????????????.
????scoped_ptr????????
????template
????class ScopedPtr
????{
????public:
????ScopedPtr(T *ptr)
????:_ptr(ptr)
????{}
????T& operator*()
????{
????return *_ptr;
????}
????T* operator->()
????{
????return _ptr;
????}
????~ScopedPtr()
????{
????if(_ptr != 0)
????{
????delete _ptr;
????_ptr=0;
????}
????}
????protected:
????ScopedPtr(ScopedPtr& sp);
????ScopedPtr& operator=(ScopedPtr& ap);
????private:
????T *_ptr;
????};
????scoped_ptr???????????????????????????????scoped_ptr???????????????????????????????private????protected???????????????????????public.???????????public?????????????????д?????????????????Ч??????????????????????????????????????????п????????????????????????public.
????shared_ptr????????
????template<typename t="">
????class SharedPtr
????{
????public:
????SharedPtr(T *ptr)
????:_ptr(ptr)
??????_pcount(new int(1))
????{
????cout<<"????"<<endl; const="" t="">& sp)
????:_ptr(sp._ptr)
??????_pcount(sp._pcount)
????{
????cout<<"????????"<<endl; t="">& operator=(SharedPtr<t>& sp)
????{
????cout<<"?????????"<<endl; _pcount="sp._ptr;" _ptr="sp._ptr;" delete="" operator-="" return="">()
????{
????return _ptr;
????}
????T& operator*()
????{
????return *_ptr;
????}
????T* Get()
????{
????return _ptr;
????}
????int GetCount()
????{
????return *_pcount;
????}
????~SharedPtr()
????{
????cout<<"??????"<<endl; _pcount="0;" _ptr="0;" delete="" int="" pre="" private:="" t="">
????shared_ptr??????????????????????????????????????????ü?????
??????shared_ptr???????????????????pcount???????????????????????????????????????????pcount???0??????????pcount?????????????????.????????shared_ptr?????????????????????????????????shared_ptr?????????????????-????????????
??????????????????
????struct Node
????{
????~Node()
????{
????cout<<"~Node()"<<endl; int="" node=""> _next;
????boost::shared_ptr<node> _prev;
????};
????void testshared_ptr() //????????????
????{
????boost::shared_ptr<node> sp1(new Node);
????boost::shared_ptr<node> sp2(new Node);
????cout<<sp1.use_count()<<endl; 1="" sp1-="">_next=sp2;
????sp2->_prev=sp1;
????cout<<sp1.use_count()<<endl; 2="" pre="">
?????????????????????????sp1??sp2??_next??_prev????shared_ptr???????????????????????????n1??n2???sp1??sp2??_prev?????n1??sp2??sp1??_next?????n2?????????shared_ptr????????ü????????????????????????????????????????????????????n1??n2???????????????????????????????????????????????ü?????????????2????????????????????????
????????????????????????????????й???????????????n1????????????????????????????????n1????n2?????ü?????????????????n2????n1?????ü???????????????????????????????????????????????????????????????????????????ò?????.????????????????????????.?????shared_ptr?е????????????????????????μ????????-weak_ptr
????struct Node
????{
????~Node()
????{
????cout<<"~Node()"< _next;
????boost::weak_ptr _prev;
????};
????void testshared_ptr() //????????????
????{
????boost::shared_ptr sp1(new Node);
????boost::shared_ptr sp2(new Node);
????cout<_next=sp2;
????sp2->_prev=sp1;
????cout<
????weak_ptr????????????????ü?????????????????weak_ptr???????????????????shared_ptr???????????weak_ptr?????????????shared_ptr????????????
??????
???·???
??????????????????
2023/3/23 14:23:39???д?ò??????????
2023/3/22 16:17:39????????????????????Щ??
2022/6/14 16:14:27??????????????????????????
2021/10/18 15:37:44???????????????
2021/9/17 15:19:29???·???????·
2021/9/14 15:42:25?????????????
2021/5/28 17:25:47??????APP??????????
2021/5/8 17:01:11