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

网站做线支付平台系统多少钱晋中网站seo

网站做线支付平台系统多少钱,晋中网站seo,在线可以做翻译的网站,html网站引导页模板智能指针 本人不才,只能将智能指针介绍一下,无法结合线程进行深入探索 介绍及作用 在异常产生进行跳转时,通过栈帧回收进行内存释放,防止内存泄漏 基于RAII思想可以创建出只能指针 RAII(Resource Acquisition Is Initializatio…

智能指针

本人不才,只能将智能指针介绍一下,无法结合线程进行深入探索

介绍及作用

在异常产生进行跳转时,通过栈帧回收进行内存释放,防止内存泄漏
基于RAII思想可以创建出只能指针
RAII(Resource Acquisition Is Initialization)——是一种利用对象控制空间生命周期的技术
这种技术好处就在于可以自动化的释放资源
智能指针——使用如指针,支持->和**针对内置数据类型->针对自定义数据类型


先介绍一个对象管理一份资源

auto_ptr
	template<class T>class auto_ptr{// auto_ptr 会产生指针悬空的情况,在进行解引用的时候很危险public:auto_ptr(T* p=nullptr) :_ptr(p) {}~auto_ptr(){puts("~auto_ptr()");delete _ptr;}auto_ptr(auto_ptr<T>& p){_ptr = p._ptr;p._ptr = nullptr;}auto_ptr<T>& operator=(auto_ptr<T>& p){// 因为是一个对象管理一份资源,比较的时候也可以使用this!=&pif (_ptr != p._ptr){if (_ptr) delete _ptr;_ptr = p._ptr;p._ptr = nullptr;}return *this;}T& operator*(){return *_ptr;}T* operator->(){return _ptr;}private:T* _ptr;};

auto_ptr会在赋值的时候将资源进行转移,并将自己置为nullptr,从而造成指针悬空的问题

unique_ptr
	template<class T>class unique_ptr{// 在auto_ptr的基础上进行优化,防拷贝public:unique_ptr(T* p = nullptr) :_ptr(p) {}~unique_ptr(){puts("~auto_ptr()");delete _ptr;}unique_ptr(const unique_ptr<T>& p) = delete;unique_ptr& operator=(const unique_ptr<T>& p) = delete;T& operator*(){return *_ptr;}T* operator->(){return _ptr;}private:// unique_ptr(const unique_ptr<T>& p);// unique_ptr& operator=(const unique_ptr<T>& p);T* _ptr;};

只是为了解决拷贝带来的指针悬空的问题——禁用拷贝构造和赋值重载
两种方式达到禁用拷贝构造和赋值重载

  1. 将拷贝构造和赋值重载定义成private
  2. 由于C++11扩展了delete的功能,可以在public中对两个函数使用delete关键字修饰

多个指针同时享有一份资源

shared_ptr

使用一个计数指针进行计数
我的代码写成这个样子是因为考虑到可能只声明指针但是没有赋值的情况,所以就需要对指针位nullptr的情况进行特殊判断

struct ListNode
{int val;bit::shared_ptr<ListNode> next;bit::shared_ptr<ListNode> prev;~ListNode(){cout << "~ListNode()" << endl;}
};template<class T>class shared_ptr	{// 删除器就是为了解决:释放数组,不是new出来的指针public:shared_ptr(T* p = nullptr) :_ptr(p), _count(new int(0)) {}template<class D>shared_ptr(T* p,D del):_ptr(p),_count(new int(1)),_del(del){if (p) (*_count)++;}~shared_ptr(){if (_ptr){//printf("~shared_ptr() -> %p\n", _ptr);if (--(*_count) == 0){delete _count;}delete _ptr;}if (_ptr == nullptr) delete _count;}shared_ptr(const shared_ptr<T>& p){if (_ptr && _ptr != p._ptr){if (--(*_count) == 0) _count = p._count;(*_count)++;_ptr = p._ptr;}else{// nullptr / 有值相等_ptr = p._ptr;_count = p._count;//*_count++; // ++优先级大于*,最好不要写这种代码(*_count)++;}}shared_ptr& operator=(const shared_ptr<T>& p){if (_ptr && _ptr != p._ptr){if (--(*_count) == 0) _count = p._count;(*_count)++;_ptr = p._ptr;}else{// nullptr / 有值相等_ptr = p._ptr;_count = p._count;(*_count)++;}return *this;}T& operator*(){return *_ptr;}T* operator->(){return _ptr;}int use_count() const {return *_count;}T* get() const{return _ptr;}private:T* _ptr;int* _count;// 引入计数指针function<void(T*)> _del = [](T* p) {delete p; };};

在这里插入图片描述
这里会产生循环引用的问题
请添加图片描述
为了解决这个问题有了weak_ptr


weak_ptr

weak_ptr只进行引用不进行计数

struct ListNode
{int val;bit::weak_ptr<ListNode> next;bit::weak_ptr<ListNode> prev;~ListNode(){cout << "~ListNode()" << endl;}
};template<class T>class weak_ptr{// 不增加引用计数public:weak_ptr() :_ptr(nullptr) {}~weak_ptr(){//printf("~shared_ptr() -> %p\n", _ptr);}weak_ptr(const shared_ptr<T>& p){_ptr = p.get();}weak_ptr& operator=(const shared_ptr<T>& p){_ptr = p.get();return *this;}T& operator*(){return *_ptr;}T* operator->(){return _ptr;}private:T* _ptr;};

在这里插入图片描述


使用包装器进行释放

这里还有一个问题——如何根据空间开的个数进行释放呢,数组和指针释放的方式是不一样的
也就是在share_ptr看不懂的版本

template<class T>
struct Del
{void operator()(T* ptr){delete[] ptr;}
};template<class T>class shared_ptr	{// 删除器就是为了解决:释放数组,不是new出来的指针public:shared_ptr(T* p = nullptr) :_ptr(p), _count(new int(0)) {}template<class D>shared_ptr(T* p,D del):_ptr(p),_count(new int(1)),_del(del){if (p) (*_count)++;}~shared_ptr(){if (_ptr){//printf("~shared_ptr() -> %p\n", _ptr);if (--(*_count) == 0){delete _count;}delete _ptr;}if (_ptr == nullptr) delete _count;}shared_ptr(const shared_ptr<T>& p){if (_ptr && _ptr != p._ptr){if (--(*_count) == 0) _count = p._count;(*_count)++;_ptr = p._ptr;}else{// nullptr / 有值相等_ptr = p._ptr;_count = p._count;//*_count++; // ++优先级大于*,最好不要写这种代码(*_count)++;}}shared_ptr& operator=(const shared_ptr<T>& p){if (_ptr && _ptr != p._ptr){if (--(*_count) == 0) _count = p._count;(*_count)++;_ptr = p._ptr;}else{// nullptr / 有值相等_ptr = p._ptr;_count = p._count;(*_count)++;}return *this;}T& operator*(){return *_ptr;}T* operator->(){return _ptr;}int use_count() const {return *_count;}T* get() const{return _ptr;}private:T* _ptr;int* _count;// 引入计数指针function<void(T*)> _del = [](T* p) {delete p; };};

仿函数,函数指针,lambda可以使用包装器——不论使用哪一种,实现的目的就是为了释放数组
他的类型一定是void(*T)
为什么需要在声明的时候就给默认到lambda——如果在这个指针是拷贝构造生成的,那还得进行包装器拷贝,同样在赋值重载的地方也需要同样的操作,还不如声明的时候给默认值

http://www.wooajung.com/news/26834.html

相关文章:

  • 专业供应的网站制作营销推广渠道有哪些
  • 做水果蔬菜生意网站合肥百度seo排名
  • 做进行网站推广赚钱安装百度到手机桌面
  • 巴中企业网站建设站长之家备案查询
  • wordpress摘要字数快排seo
  • 新闻网站运做国产免费crm系统有哪些
  • 网站备案幕布可以ps么必应站长平台
  • 计算机技术移动互联网开发seo综合查询工具
  • 做外贸主页网站用什么的空间好点信阳seo推广
  • 国家工业和信息化部网站备案系统emlog友情链接代码
  • 做网站可以提些什么意见百度排名优化工具
  • 建一个类似b站的网站多少钱销售新手怎么找客源
  • 图片库网站建设长沙seo关键词排名
  • 兰州网站优化软件网站服务器ip查询
  • wordpress主页出现副标题seo免费课程
  • 移动端网站开发注意些什么百度官方网站网址是多少
  • 导航网站怎么做的怎样做网络推广挣钱
  • 2018做网站还赚钱吗百度安装
  • 如何做网站窗口网站排名seo软件
  • 网站建设重庆最加科技杭州seo排名公司
  • 佛山网站建设网站制作公司哪家好佛山网站排名提升
  • wordpress付费阅读文章功能关键词seo培训
  • 广州网站改版电商培训班
  • 档案网站建设网页360seo排名优化服务
  • 肇庆网站建设维护网站可以自己建立吗
  • 自己怎么设计公司logoseo搜索引擎优化期末考试
  • 做网站字体用什么格式郑州网站建设推广有限公司
  • ppt模板下载网站推荐医院网络销售要做什么
  • 私人可以做慈善网站吗google图片搜索
  • 贸易公司网站案例如何利用网络进行推广和宣传