当前位置: 首页 > news >正文

建设部网站官网设计排版网站

建设部网站官网,设计排版网站,王建设医生网站,网站开发要注意的漏洞实现自己的智能指针 //智能指针 保证能做到资源的自动释放 //利用栈上的对象出作用域自动析构的特征#xff0c;来做到资源的自动释放的 templatetypename T class CSmartPtr { public:CSmartPtr(T *ptr nullptr):mptr(ptr) {}~CSmartPtr() { delete mptr; } privat…实现自己的智能指针 //智能指针 保证能做到资源的自动释放 //利用栈上的对象出作用域自动析构的特征来做到资源的自动释放的 templatetypename T class CSmartPtr { public:CSmartPtr(T *ptr nullptr):mptr(ptr) {}~CSmartPtr() { delete mptr; } private:T *mptr; };int main() {CSmartPtrint ptr1(new int);return 0; }不能把智能指针new在堆上 CSmartPtrint *p new CSmartPtrint(new int); //p还是裸指针重载*和- //智能指针 保证能做到资源的自动释放 //利用栈上的对象出作用域自动析构的特征来做到资源的自动释放的 templatetypename T class CSmartPtr { public:CSmartPtr(T *ptr nullptr):mptr(ptr) {}~CSmartPtr() { delete mptr; }T operator*() { return *mptr; }T* operator-() { return mptr; } private:T *mptr; };int main() {CSmartPtrint ptr1(new int);class Test{public:void test() { cout Test::test() endl; }};CSmartPtrTest test(new Test());test-test();return 0; }/*不带引用计数的智能指针auto_ptr:C库里面C11新标准scoped_ptrunique_ptr*/auto_ptrint ptr1(new int);auto_ptrint ptr2(ptr1);*ptr2 20;cout *ptr1 endl; //ptr1现在为空//不推荐使用auto_ptr//scoped_ptr //scoped_ptr(const scoped_ptrT) delete; //scoped_ptrT operator(const scoped_ptrT) delete;unique_ptrunique_ptr(const unique_ptrT) delete;unique_ptrT operator(const unique_ptrT) delete;//实现了右值引用的拷贝unique_ptr(unique_ptrT src)unique_ptrT operator(unique_ptrT src)templatetypename Tunique_ptrT getSmartPtr(){unique_ptrT ptr(new T());return ptr;}unique_ptrint ptr1 getSmartPtrint();unique_ptrint p1(new int);unique_ptrint p2(std::move(p1));引用计数 //对资源进行引用计数的类 templatetypename T class RefCnt { public:RefCnt(T *ptr nullptr):mptr(ptr){if (mptr ! nullptr)mcount 1;}//增加资源的引用计数void addRef(){mcount;}int delRef() { return --mcount; } private:T *mptr;int mcount; };//智能指针 保证能做到资源的自动释放 //利用栈上的对象出作用域自动析构的特征来做到资源的自动释放的 templatetypename T class CSmartPtr { public:CSmartPtr(T *ptr nullptr):mptr(ptr) {mpRefCnt new RefCntT(mptr);}~CSmartPtr() { if (0 mpRefCnt-delRef()){delete mptr; mptr nullptr;}}T operator*() { return *mptr; }T* operator-() { return mptr; }CSmartPtr(const CSmartPtrT src):mptr(src.mptr), mpRefCnt(src.mpRefCnt){if (mptr ! nullptr)mpRefCnt-addRef();}CSmartPtrT operator(const CSmartPtrT src){if (this src)return *this;if (0 mpRefCnt-delRef()){delete mptr;}mptr src.mptr;mpRefCnt src.mpRefCnt;mpRefCnt-addRef();return *this;} private:T *mptr; //指向资源的指针RefCntT *mpRefCnt; //指向该资源引用计数对象的指针 }; int main() {CSmartPtrint ptr1(new int);CSmartPtrint ptr2(ptr1);CSmartPtrint ptr3;ptr3 ptr2;*ptr1 20;cout *ptr2 *ptr3 endl;return 0; }循环引用 class B; class A { public:A() { cout A() endl; }~A() { cout ~A() endl; }shared_ptrB _ptrb; };class B { public:B() { cout B() endl; }~B() { cout ~B() endl; }shared_ptrA _ptra; };int main() {shared_ptrA pa(new A());shared_ptrB pb(new B());pa-_ptrb pb;pb-_ptra pa;cout pa.use_count() endl;cout pb.use_count() endl;return 0; }定义对象的时候用强智能指针引用对象的地方使用弱智能指针 class B; class A { public:A() { cout A() endl; }~A() { cout ~A() endl; }weak_ptrB _ptrb; };class B { public:B() { cout B() endl; }~B() { cout ~B() endl; }weak_ptrA _ptra; };int main() {shared_ptrA pa(new A());shared_ptrB pb(new B());pa-_ptrb pb;pb-_ptra pa;cout pa.use_count() endl;cout pb.use_count() endl;return 0; }weak_ptr只是观察作用观察资源还可不可用但是却不能用 class B; class A { public:A() { cout A() endl; }~A() { cout ~A() endl; }void testA() { cout 非常好用的方法 endl; }weak_ptrB _ptrb; };class B { public:B() { cout B() endl; }~B() { cout ~B() endl; }void func(){_ptra-testA();//使用不了}weak_ptrA _ptra; };在使用时必须进行提升 class B { public:B() { cout B() endl; }~B() { cout ~B() endl; }void func(){//_ptra-testA();//使用不了shared_ptrA ps _ptra.lock(); //提升方法if (ps ! nullptr){ps-testA();}}weak_ptrA _ptra; };//多线程访问共享对象的线程安全问题 class A { public:A() { cout A() endl; }~A() { cout ~A() endl; }void testA() { cout 非常好用的方法 endl; } };void handler01(A *q) {std::this_thread::sleep_for(std::chrono::seconds(2));//q访问A对象的时候需要侦测一下A对象是否存活q-testA(); }int main() {A *p new A();thread t1(handler01, p);delete p;t1.join();return 0; }class A { public:A() { cout A() endl; }~A() { cout ~A() endl; }void testA() { cout 非常好用的方法 endl; } };void handler01(weak_ptrA pw) {std::this_thread::sleep_for(std::chrono::seconds(2));shared_ptrA sp pw.lock();if (sp ! nullptr){sp-testA();}else{cout A对象已经析构不能访问 endl;} }int main() {{shared_ptrA p(new A());thread t1(handler01, weak_ptrA(p));t1.detach();}std::this_thread::sleep_for(std::chrono::seconds(20));return 0; }/* 智能指针的删除器 deletor 智能指针能够保证资源绝对的释放 */ //unique_ptr shared_ptr /* ~unique_ptr() {是一个函数对象的调用 deletor(ptr); } templatetypename T class Deletor { public:void operator()(T *ptr){delete ptr;} }; */templatetypename T class MyDeletor { public:void operator()(T *ptr) const{cout call MyDeletor.operator() endl;delete []ptr;} };int main() {unique_ptrint, MyDeletorint ptr1(new int[100]);return 0; }templatetypename T class MyFileDeletor { public:void operator()(T *ptr) const{cout call MyFileDeletor.operator() endl;fclose(ptr);} };int main() {unique_ptrFILE, MyFileDeletorFILE ptr(fopen(data.txt, w));return 0; }使用lambda unique_ptrint, functionvoid(int*) ptr1(new int[100],[](int *p)-void {cout call lambda release new int[100] endl;delete[]p;});unique_ptrFILE, functionvoid(FILE*) ptr2(fopen(data.txt, w),[](FILE *p)-void {cout call lambda release fopen endl;fclose(p);});
http://www.zqtcl.cn/news/689910/

相关文章:

  • 绍兴兴住房和城乡建设局网站网站更换名称需要重新备案吗
  • 跨境电商网站开发文档网站建设费可摊几年
  • 怎样建设一个游戏网站随便玩玩在线制作网站
  • 免费的成品网站用织梦模板做网站
  • 彩票网站开发 极云有的域名怎样做网站
  • 网店运营推广网站买个天猫店多少钱一个
  • 资讯网站排版广告公司取名大全集
  • 织梦网站seo安徽建设厅网站
  • 北京智能模板建站如何增加网站的索引量
  • 哪个网站专做进口商品的网站备案好麻烦
  • 南京网站制作哪家专业接口网站开发
  • 网站正在建设中9797鲜花网页设计模板
  • wordpress怎么自动更新网站地图现在最流行的网站开发工具
  • 科技局网站查新怎么做vs网站制作教程
  • 网站开发流程文档东莞英文建站公司
  • 怎样建俄文网站wordpress国产主题推荐
  • 网站开发晋升空间 路径秦皇岛房管局官网
  • 中山网站建设sipocms做家电网站好
  • 石家庄建设局网站怎么打不开手机网站素材
  • 电影网站怎么做要多少钱中企动力 网站价格
  • 长沙企业如何建网站爱用建站
  • 在哪个网站上做实验仪器比较好农村住宅设计图集
  • 网站的源代码有什么用wordpress英文博客模板下载
  • 用html5做网站的优点国内家居行业网站开发
  • 临沂企业网站客流统计系统厂家
  • 深圳H5网站开发最新版app下载安装
  • 手机网站免费模板下载成都建设项目环境影响登记网站
  • 上海网站seo公司网站建设公司盈利分析
  • 影评网站怎么做培训总结心得体会
  • 做微站比较好的网站注册子公司流程及所需资料