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

商城网站开发报平面设计培训哪个好

商城网站开发报,平面设计培训哪个好,建设网站需要什么技术支持,有什么做网站好用的软件C在用引用取缔掉指针的同时#xff0c;模板的引入带给了指针新的发挥空间 智能指针简单的来说就是带有不同特性和内存管理的指针模板 unique_ptr 1.不能有多个对象指向一块内存 2.对象释放时内部指针指向地址也随之释放 3.对象内数据只能通过接口更改绑定 4.对象只能接收右值…C在用引用取缔掉指针的同时模板的引入带给了指针新的发挥空间 智能指针简单的来说就是带有不同特性和内存管理的指针模板 unique_ptr 1.不能有多个对象指向一块内存 2.对象释放时内部指针指向地址也随之释放 3.对象内数据只能通过接口更改绑定 4.对象只能接收右值或者将亡值shared_ptr 1.可以有多个指针对象指向一块地址 2.使用一块堆区空间维护一块地址被指向的次数 3.当指向一个地址的指针数量变为0时清除这块空间weak_ptr 和shared_ptr搭配使用解决了循环引用问题 首先unique_ptr实现起来其实很简单只需要对赋值函数运算符重载和拷贝构造函数等这些利用对象创建新的对象的函数做出修改即可当然析构函数部分的释放也是要注意的。 templatetypename T class unique_ptr{T*ptr; public:unique_ptr(T*pnullptr):ptr(p){}unique_ptr(unique_ptrother) noexcept:ptr(other.release()){other.ptrnullptr;}unique_ptr operator(unique_ptrother) noexcept{if(other.ptr!this-ptr){change(other.release());}return *this;}T*get()const{return ptr;}T operator*() const{return *ptr;}T* operator-() const{return ptr;}explicit operator bool() const{return ptr!nullptr;}~unique_ptr(){delete ptr;ptrnullptr;}T*release(){T*tmpthis-ptr;this-ptrnullptr;return tmp;}void change(T*curnullptr){if(cur!ptr){delete ptr;ptrcur;}}unique_ptr(const unique_ptr)delete;unique_ptr operator(const unique_ptr)delete; };然后看shared_ptr它的话要比unique_ptr要自由的多实现起来也很简单只需要多一步计数操作即可。 templatetypename T class shared_ptr{T*ptr;int* cnt; public:shared_ptr(T*pnullptr):ptr(p),cnt(new int(1)){}shared_ptr(const shared_ptrother) noexcept:ptr(other.ptr),cnt(other.cnt){(*cnt);}shared_ptr operator(const shared_ptrother) noexcept{if(this!other){release();this-ptrother.ptr;this-cntother.cnt;(*cnt);}return *this;}T*get()const{return ptr;}T operator*() const{return *ptr;}T* operator-() const{return ptr;}explicit operator bool() const{return ptr!nullptr;}~unique_ptr(){release();}void release(){if(cnt--(*cnt)0){delete cnt;delete ptr;}ptrnullptr;cntnullptr;} }; 但是注意shared_ptr的计数可能会带来一个问题循环引用 看下面代码其实很好理解就是循环指向造成计数到不了0从而释放不了对象。 class B; class A{ public:shared_ptrBaptr; }; class B{ public:shared_ptrAbptr; }; int main(){shared_ptrAanew A;shared_ptrBbnew B;a-aptrb;b-bptra; }解决方式引入了weak_ptr搭配shared_ptr使用weak_ptr是对对象的一种弱引用它不会增加对象的use_countweak_ptr和shared_ptr可以相互转化shared_ptr可以直接赋值给weak_ptrweak_ptr也可以通过调用lock函数来获得shared_ptr。 weak_ptr指针通常不单独使用只能和 shared_ptr 类型指针搭配使用。将一个weak_ptr绑定到一个shared_ptr不会改变shared_ptr的引用计数。一旦最后一个指向对象的shared_ptr被销毁对象就会被释放。即使有weak_ptr指向对象对象也还是会被释放。weak_ptr并没有重载operator-和operator *操作符因此不可直接通过weak_ptr使用对象典型的用法是调用其lock函数来获得shared_ptr示例进而访问原始对象。需要注意weak_ptr不维护资源的释放因为需要避免资源被释放两次因此weak_ptr常用在为生命周期一致的shared_ptr赋值 template typename T class MySharedPtr;template typename T class MyWeakPtr;template typename T class MySharedPtr { private:T* ptr;int* refCount;int* weakCount;public:explicit MySharedPtr(T* p nullptr) : ptr(p), refCount(new int(1)), weakCount(new int(0)) {}MySharedPtr(const MySharedPtr other) : ptr(other.ptr), refCount(other.refCount), weakCount(other.weakCount) {(*refCount);}~MySharedPtr() {release();}int use_count() const {return (refCount ? *refCount : 0);}T* get() const {return ptr;}T operator*() const {return *ptr;}T* operator-() const {return ptr;}MyWeakPtrT weak_ptr() {(*weakCount);return MyWeakPtrT(this-ptr, this-refCount, this-weakCount);}MySharedPtr operator(const MySharedPtr other) {if (this ! other) {release();ptr other.ptr;refCount other.refCount;weakCount other.weakCount;(*refCount);}return *this;}private:void release() {if (refCount) {(*refCount)--;if (*refCount 0) {delete ptr;delete refCount;if (*weakCount 0) {delete weakCount;}}}}friend class MyWeakPtrT; };template typename T class MyWeakPtr { private:T* ptr;int* refCount;int* weakCount;public:MyWeakPtr() : ptr(nullptr), refCount(nullptr), weakCount(nullptr) {}MyWeakPtr(T* p, int* rc, int* wc) : ptr(p), refCount(rc), weakCount(wc) {}MyWeakPtr(const MyWeakPtr other) : ptr(other.ptr), refCount(other.refCount), weakCount(other.weakCount) {(*weakCount);}~MyWeakPtr() {release();}MySharedPtrT lock() {if (expired()) {return MySharedPtrT();}(*refCount);return MySharedPtrT(ptr, refCount, weakCount);}MyWeakPtr operator(const MyWeakPtr other) {if (this ! other) {release();ptr other.ptr;refCount other.refCount;weakCount other.weakCount;(*weakCount);}return *this;}bool expired() const {return (refCount nullptr || *refCount 0);} private:void release() {if (weakCount) {(*weakCount)--;if (*weakCount 0) {delete weakCount;}}} };shared_ptr和weak_ptr可以互相构造看下面的代码就知道它们是如何解决循环引用的情况 class B; class A{ public:MYShared_PtrBaptr; }; class B{ public:MYWeak_PtrAbptr; }; int main(){MYShared_PtrAanew A;MYShared_PtrBbnew B;a-aptrb.lock();b-bptra; }简单的来说就是通过两个可以互相转换的类型避免产生同种类型的环对不同类型的环它们释放的时候互不影响对方的计数所以可以正常释放。
http://www.w-s-a.com/news/732123/

相关文章:

  • 成都网站开发费用做行程的网站
  • 做地铁建设的公司网站手机网站首页布局设计
  • 福建亨立建设集团有限公司网站搜狗网页游戏大厅
  • 设计网站musil访问量大的网站选择多少流量的服务器何时
  • 公司网站包括哪些内容新网站怎样做外链
  • 淘宝宝贝链接怎么做相关网站广州好蜘蛛网站建设
  • 长春网站制作网页博山区住房和城乡建设局网站
  • 云南大学网站建设解析到网站怎样做
  • 网站维护的要求包括锦溪网站建设
  • 金站网.营销型网站学校安全教育网站建设
  • 临沂市建设局网站公示军事新闻头条2023
  • 购物网网站建设lamp 做网站
  • 做网站网站庄家html5网站开发技术
  • 无锡门户网站制作电话广告设计公司的未来
  • 白云区专业网站建设网页设计模拟试题答案
  • 毕业设计网站代做多少钱制作旅游网站设计概述
  • 网站开发维护运维无人在线电视剧免费观看
  • 电子商务网站建设开题报告展馆网站建设
  • 门户网站建设的背景和意义手机网站前
  • 国内免费视频素材无水印素材网站国家最新消息
  • 襄阳seo站内优化学做网站论坛教程
  • 文明网站建设情况报告wordpress伪静态配置
  • 牙科网站模板个人微信网站建设
  • 厦门公司注册网站dw做简单小说网站
  • 网站建好以后每年都续费么wordpress 仿聚划算
  • 单位网站建设收费标准网上开店铺需要多少钱
  • 灯饰网站需要这么做申请域名的流程
  • 软件下载网站怎么赚钱wordpress减少数据库查询
  • 什么兼职网站可以做视频剪辑常见的推广平台有哪些
  • 网站开发是用html还是jsp设迹官网