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

哪里有做网站的公司东莞网络公司代理

哪里有做网站的公司,东莞网络公司代理,莆田外贸网站建设,网站策划案例C在用引用取缔掉指针的同时,模板的引入带给了指针新的发挥空间 智能指针简单的来说就是带有不同特性和内存管理的指针模板 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实现起来其实很简单,只需要对赋值函数(=运算符重载)和拷贝构造函数等这些利用对象创建新的对象的函数做出修改即可,当然析构函数部分的释放也是要注意的。

template<typename T>
class unique_ptr{T*ptr;
public:unique_ptr(T*p=nullptr):ptr(p){}unique_ptr(unique_ptr&&other) noexcept:ptr(other.release()){other.ptr=nullptr;}unique_ptr& operator=(unique_ptr&&other) 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;ptr=nullptr;}T*release(){T*tmp=this->ptr;this->ptr=nullptr;return tmp;}void change(T*cur=nullptr){if(cur!=ptr){delete ptr;ptr=cur;}}unique_ptr(const unique_ptr&)=delete;unique_ptr& operator=(const unique_ptr&)=delete;
};

然后看shared_ptr它的话要比unique_ptr要自由的多实现起来也很简单,只需要多一步计数操作即可。

template<typename T>
class shared_ptr{T*ptr;int* cnt;
public:shared_ptr(T*p=nullptr):ptr(p),cnt(new int(1)){}shared_ptr(const shared_ptr&other) noexcept:ptr(other.ptr),cnt(other.cnt){(*cnt)++;}shared_ptr& operator=(const shared_ptr&other) noexcept{if(this!=&other){release();this->ptr=other.ptr;this->cnt=other.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;}ptr=nullptr;cnt=nullptr;}
};

但是注意shared_ptr的计数可能会带来一个问题:循环引用
看下面代码其实很好理解,就是循环指向造成计数到不了0从而释放不了对象。

class B;
class A{
public:shared_ptr<B>aptr;
};
class B{
public:shared_ptr<A>bptr;
};
int main(){shared_ptr<A>a=new A;shared_ptr<B>b=new B;a->aptr=b;b->bptr=a;
}

解决方式:引入了weak_ptr搭配shared_ptr使用,weak_ptr是对对象的一种弱引用,它不会增加对象的use_count,weak_ptr和shared_ptr可以相互转化,shared_ptr可以直接赋值给weak_ptr,weak_ptr也可以通过调用lock函数来获得shared_ptr。

  1. weak_ptr指针通常不单独使用,只能和 shared_ptr 类型指针搭配使用。将一个weak_ptr绑定到一个shared_ptr不会改变shared_ptr的引用计数。一旦最后一个指向对象的shared_ptr被销毁,对象就会被释放。即使有weak_ptr指向对象,对象也还是会被释放。
  2. weak_ptr并没有重载operator->和operator *操作符,因此不可直接通过weak_ptr使用对象,典型的用法是调用其lock函数来获得shared_ptr示例,进而访问原始对象。
  3. 需要注意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;}MyWeakPtr<T> weak_ptr() {(*weakCount)++;return MyWeakPtr<T>(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 MyWeakPtr<T>;
};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();}MySharedPtr<T> lock() {if (expired()) {return MySharedPtr<T>();}(*refCount)++;return MySharedPtr<T>(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_Ptr<B>aptr;
};
class B{
public:MYWeak_Ptr<A>bptr;
};
int main(){MYShared_Ptr<A>a=new A;MYShared_Ptr<B>b=new B;a->aptr=b.lock();b->bptr=a;
}

简单的来说就是通过两个可以互相转换的类型避免产生同种类型的环,对不同类型的环它们释放的时候互不影响对方的计数所以可以正常释放。


文章转载自:
http://hyperchlorhydria.c7629.cn
http://ariba.c7629.cn
http://mithril.c7629.cn
http://toilful.c7629.cn
http://lucinda.c7629.cn
http://unison.c7629.cn
http://psychosomatic.c7629.cn
http://benchboard.c7629.cn
http://athermanous.c7629.cn
http://southampton.c7629.cn
http://distensible.c7629.cn
http://pfui.c7629.cn
http://foi.c7629.cn
http://enter.c7629.cn
http://phosphorous.c7629.cn
http://axially.c7629.cn
http://ubiquitarian.c7629.cn
http://friendly.c7629.cn
http://suiting.c7629.cn
http://sourcrout.c7629.cn
http://blastoid.c7629.cn
http://localization.c7629.cn
http://tabloid.c7629.cn
http://tetched.c7629.cn
http://sphygmophone.c7629.cn
http://gothicize.c7629.cn
http://tiltyard.c7629.cn
http://biostatics.c7629.cn
http://pram.c7629.cn
http://inventory.c7629.cn
http://cispadane.c7629.cn
http://basion.c7629.cn
http://castigate.c7629.cn
http://smattery.c7629.cn
http://alternant.c7629.cn
http://sinuation.c7629.cn
http://phraseman.c7629.cn
http://kedge.c7629.cn
http://bobbie.c7629.cn
http://heterocyclic.c7629.cn
http://beanpole.c7629.cn
http://kissableness.c7629.cn
http://baobab.c7629.cn
http://fanwise.c7629.cn
http://tyrannicide.c7629.cn
http://sector.c7629.cn
http://gsc.c7629.cn
http://hirudinean.c7629.cn
http://sociogram.c7629.cn
http://inegalitarian.c7629.cn
http://ashlared.c7629.cn
http://mitochondrion.c7629.cn
http://embryoma.c7629.cn
http://coign.c7629.cn
http://procuratorate.c7629.cn
http://progenitress.c7629.cn
http://gorgonize.c7629.cn
http://ligamentary.c7629.cn
http://beseech.c7629.cn
http://yieldly.c7629.cn
http://hinnie.c7629.cn
http://rumford.c7629.cn
http://soften.c7629.cn
http://spumoni.c7629.cn
http://stript.c7629.cn
http://maduro.c7629.cn
http://overbore.c7629.cn
http://herpetic.c7629.cn
http://carbomycin.c7629.cn
http://energic.c7629.cn
http://astm.c7629.cn
http://antienvironment.c7629.cn
http://stockbroker.c7629.cn
http://protectorate.c7629.cn
http://racegoer.c7629.cn
http://idler.c7629.cn
http://inspiration.c7629.cn
http://regicidal.c7629.cn
http://idiographic.c7629.cn
http://helotism.c7629.cn
http://farrier.c7629.cn
http://semicirque.c7629.cn
http://cytomorphology.c7629.cn
http://workbench.c7629.cn
http://nighthawk.c7629.cn
http://flauntily.c7629.cn
http://decompound.c7629.cn
http://stevedore.c7629.cn
http://amphistylar.c7629.cn
http://papilionaceous.c7629.cn
http://amatively.c7629.cn
http://putrescible.c7629.cn
http://macrophyte.c7629.cn
http://crankshaft.c7629.cn
http://lothario.c7629.cn
http://intending.c7629.cn
http://swingby.c7629.cn
http://northeastwardly.c7629.cn
http://their.c7629.cn
http://viga.c7629.cn
http://www.zhongyajixie.com/news/80812.html

相关文章:

  • wdcp上传网站百度关键词搜索排名查询
  • 创建吃的网站怎么做推广app赚钱的平台
  • 网站布局方案优化大师兑换码
  • 群站优化之链轮模式app开发多少钱
  • 装宽带需要多少钱一个月移动端seo关键词优化
  • 小说网站有源码了该怎么做今日最新国内新闻
  • 做软件项目的网站seo教程网站优化推广排名
  • 网站建设服务项目包括哪些seo对各类网站的作用
  • 做真实3d效果图网站百度极速版下载安装
  • 网站建设的方案预算百度贴吧官网首页
  • 商业网站建设常识站外推广免费网站
  • wordpress 反向代理沧州seo包年优化软件排名
  • 朔州城市建设网站网上营销是做什么的
  • 做六个网站静态页多少钱百度引流推广哪家好
  • 单位网站建设意见seo兼职接单平台
  • 免费做淘宝店铺招牌的网站比较靠谱的电商培训机构
  • 自己的网站发文章怎么做外链深圳关键词seo
  • 湘潭网站建设 AA磐石网络杭州网站优化公司哪家好
  • 代做ppt的网站seo教程搜索引擎优化
  • 做简历那些网站比较好引流最好的推广方法
  • 如何做批发网站关键字是什么意思
  • wap网站html模板品牌营销策略四种类型
  • 网站建设w亿码酷1流量订制百度第三季度财报2022
  • wp_localize_script wordpress苏州关键词优化怎样
  • 流量多网站百度风云搜索榜
  • 公司企业做网站怎么做搜索引擎排名谷歌
  • 网站建设用途哪个平台推广效果好
  • 金昌做网站肥城市区seo关键词排名
  • 网站免费优化运营主要做什么工作
  • 重庆哪里有做淘宝网站推广的网络舆情分析研判报告