C++复习笔记23
创始人
2025-05-30 14:17:11
0
      RAII(Resource Acquisition Is Initialization)是一种利用对象生命周期来控制程序资源(如内存、文件句柄、网络连接、互斥量等等)的简单技术。 在对象构造时获取资源,接着控制对资源的访问使之在对象的生命周期内始终保持有效,最后在对象析构的 时候释放资源。借此,我们实际上把管理一份资源的责任托管给了一个对象。这种做法有两大处: 不需要显式地释放资源。 采用这种方式,对象所需的资源在其生命期内始终保持有效。
#include
#include
#include
//auto_ptr 自动指针 VC
//auto_ptr Linux vs
//boost scoped_ptr scoped_array shared_array weak_ptr instrusive_ptr
//C11 unique_ptr shared_ptrusing namespace std;void test01()
{int* ptr = new int(10);auto_ptr pa(ptr);//智能指针//delete ptr;cout << *pa << endl;string* pstr = new string("abcxyz");auto_ptr ps(pstr);cout << ps->size() << endl;cout << "end." << endl;
}//boost
//寻找第三方代理
//智能 指针
void main()
{test01();//system("pause");
}
      指针可以解引用,也可以通过->去访问所指空间中的内容,因此:AutoPtr模板类中还得需要将* ->重载下,才可让其像指针一样去使用。       总结一下智能指针的原理: 1. RAII特性 2. 重载operator*和opertaor->,具有像指针一样的行为。 auto_ptr在VC6的底层实现
#include
#include
using namespace std;namespace hym
{templateclass auto_ptr{public:auto_ptr(_Ty* _P = 0) : _Ptr(_P), _Owns(_P != 0){}//auto_ptr(auto_ptr<_Ty>& _Y)//{// this->_Ptr = _Y._Ptr;// this->_Owns = _Y._Owns;// _Y._Owns = false;//}auto_ptr(const auto_ptr<_Ty>& _Y) :_Owns(_Y._Owns), _Ptr(_Y.release()){}auto_ptr<_Ty>& operator=(const auto_ptr<_Ty>& _Y){if (this != &_Y){if (_Ptr != _Y.get()){if (_Owns)delete _Ptr;_Owns = _Y._Owns;}else if (_Y._Owns)_Owns = true;_Ptr = _Y.release();}return (*this);}~auto_ptr(){if (_Owns)delete _Ptr;}//_Ty* release(const auto_ptr* const this)//_Ty* release()const//{//	((auto_ptr<_Ty>*)this)->_Owns = false;//	return _Ptr;//}//_Ty* get() const//{//	return (_Ptr);//}//_Ty& operator*()//{//	return (*get()); //*_Ptr//}//_Ty* operator->()//{//	return _Ptr;//}private:bool _Owns; //拥有权 true false_Ty* _Ptr;  //管理空间};
};class Test
{public:Test(int a = 0) :m_a(a) {}void fun(){cout << "m_a=" << m_a << endl;}private:int m_a;
};void test01()
{int* ptr = new  int(10);auto_ptr pa(ptr);auto_ptr pb;cout << *pb << endl;
}void test02()
{int* ptr = new int(10);int* ptr1 = new int(10);hym::auto_ptr pa=ptr;hym::auto_ptr pa1=ptr1;//微妙 而断丝连hym::auto_ptr pa2 = pa1;pa1 = pa;*pa = 100;cout << *pa << endl;cout << *pa1 << endl;//vc6版本的拷贝构造之后原来的对象仍然可以使用
}void test03()
{Test* pt = new Test(50);hym::auto_ptr pa(pt);pa->fun();
}void test04()
{int* ptr = new int(10);auto_ptr pa(ptr);auto_ptr pa1 = pa;cout << *pa1 << endl;//cout << *pa << endl;//VS版本拷贝构造后原来的对象无法使用
}
void main()
{test04();
}

auto_ptr在VS的底层实现:

#include
#include
using namespace std;namespace hym
{templateclass auto_ptr{typedef auto_ptr<_Ty> _Myt;public:explicit auto_ptr(_Ty* Ptr = 0) :_Myptr(Ptr) {}auto_ptr(_Myt& _Right) :_Myptr(_Right.release()) {}_Myt& operator=(_Myt& _Right){reset(_Right.release());return *this;}_Ty* get() const{return (_Myptr);}_Ty& operator*(){return (*get()); //*_Ptr}_Ty* operator->(){return _Myptr;}~auto_ptr(){delete _Myptr;}_Ty* release(){_Ty* _Tmp = _Myptr;_Myptr = nullptr;return _Tmp;}void reset(_Ty* _Ptr = 0){if (_Ptr != _Myptr)delete _Myptr;_Myptr = _Ptr;}private:_Ty* _Myptr;};
}void test01()
{int* ptr = new int(10);hym::auto_ptr pa(ptr);//hym::auto_ptr pa1 = pa;//cout << *pa << endl;cout << *pa<< endl;int* q = new int(100);pa.reset(q);pa.reset(q);//pa.reset(nullptr);//pa.release();int* tmp = pa.release();delete tmp;
}void test02()
{int* ptr = new int(10);int* ptr1 = new int(50);auto_ptr pa(ptr);auto_ptr pa1(ptr1);pa1 = pa;cout << "*pa1="<<*pa1 << endl;//cout << "*pa=" << *pa << endl;
}void main()
{test02();//system("pause");
}

boost库的scoped_ptr:所有权不能发生转移。

#include
#include
#include
using namespace std;
using namespace boost;class Test
{
public:void fun(){cout << "Test.fun()" << endl;}
};void test01()
{int* ptr = new int(10);scoped_ptr ps(ptr);cout << *ps << endl;Test* pt1 = new Test;scoped_ptr ps2(pt1);ps2->fun();
}void test02()
{ int* ptr = new int(10);scoped_ptr ps(ptr);//scoped_ptr ps1 = ps;//拥有权不能转移scoped_ptr ps2;//ps2 = ps1;//所有权不能转移
}void test03()
{int* ptr = new int(10);auto_ptr pa(ptr);scoped_ptr ps(pa);
}void main()
{test03();
}

相关内容

热门资讯

原创 中... 据中国青年报报道,近日,中国四艘海警船编队进入钓鱼岛海域进行常规巡航,依照既定的维权程序,船队在海域...
原创 特... 2025年11月9日,美国总统特朗普在自己的社交平台TruthSocial上宣布,他提名约翰·科尔担...
top等级胡瑾刑事律师团队:死... 在刑事法律领域,辩护律师的专业能力与经验直接关乎当事人的合法权益能否得到充分保障。随着法治建设的深入...
霸王茶姬90后创始人将成常州女... 来源:一波说传承有道 近日,一场即将举行的婚礼悄然成为财经圈与大众舆论场共同关注的焦点。 一张流传于...
常州法院2025年前三季度调解... 调解结案16474件、调解成功率24.08%——这是2025年前三季度常州法院交出的司法成绩单。通过...
安徽省政协研究室副主任陈鑫已任... 据铜陵市政府官网消息,11月20日上午,市委举行理论学习中心组学习会议,邀请省委社会工作部副部长高维...
原创 联... 据光明网报道,11月19日,在联合国大会的讨论中,日本企图争取成为安理会常任理事国的梦想再次破灭,令...
南部关于全县规范法律咨询服务机... 一、专项行动时间 自即日起至2025年12月。 二、举报受理范围 社会各界反映强烈的某些法律咨询服务...
“男子持刀入室盗窃”视频引发关... 近日,一段疑似“小偷”入室盗窃被业主家中监控拍下的视频在网上引发关注。11月21日晚,“翠屏公安”微...