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

怎么做动态的实时更新的网站什么是百度搜索推广

怎么做动态的实时更新的网站,什么是百度搜索推广,坡头网站开发公司,深圳坂田做网站智能指针share_ptr,与unique_ptr不同&#xff0c;多个shar_ptr对象可以共同管理一个指针&#xff0c;它们通过一个共同的引用计数器来管理指针。当一个智能指针对象销毁时&#xff0c;计数器减一。当计数器为0时&#xff0c;会将所指向的内存对象释放。 #include<memory>…

智能指针share_ptr,与unique_ptr不同,多个shar_ptr对象可以共同管理一个指针,它们通过一个共同的引用计数器来管理指针。当一个智能指针对象销毁时,计数器减一。当计数器为0时,会将所指向的内存对象释放。

#include<memory>
#include<iostream>
#include<vector>
#include<algorithm>
#include<chrono>
using namespace std;class Rectangle {public:Rectangle(double w, double h) :width(w), height(h) {}~Rectangle() {}double area(){return width * height;}
private:double width;double height;
};
int main()
{using std::shared_ptr;shared_ptr<Rectangle>p1(new Rectangle(1, 5));shared_ptr<Rectangle>p2 = p1;shared_ptr<Rectangle>p3(p2);cout << p1.use_count() << endl;
}

打印结果 

 另外share_ptr提供几个辅助函数,用于对封装指针类型得静态转换,动态转换以及常量转换,它们分别是dynamic_pointer_cast,static_pointer_cast和const_point_cast.

#include<memory>
#include<iostream>
#include<vector>
using namespace std;
class Base {
public:virtual ~Base() {}};
class Derive :public Base {};int main()
{shared_ptr<Base>sp1(new Derive());shared_ptr<Derive>sp2 = dynamic_pointer_cast<Derive>(sp1);shared_ptr<Base>sp3 = static_pointer_cast<Base>(sp2);cout << "use count:" << sp1.use_count() << endl;

虽然他们封装得对象类型不同,但他们都是指向同一个对象,所以引用计数为3.

在使用share_ptr时容易出现一个问题,就是当出现循环引用时,无法释放所指向的资源。造成内存泄漏。

#include<memory>
#include<iostream>
#include<vector>
using namespace std;
class Person
{
public:Person(string name) :m_name(name) {	cout << m_name << "constructed" << endl;}~Person(){cout << m_name << "desconstructed" << endl;}void setParter(const shared_ptr<Person>& other){m_partner = other;}
private:string m_name;shared_ptr<Person>m_partner;
};int main()
{vector<shared_ptr<Person>>people;people.push_back(shared_ptr<Person>(new Person("张三")));people.push_back(shared_ptr<Person>(new Person("李四")));people.push_back(shared_ptr<Person>(new Person("王五")));people[0]->setParter(people[1]);people[1]->setParter(people[2]);people[2]->setParter(people[0]);return 0;
}

打印结果

这里people存放了三个person对象,由于person 的成员变量m_partner也是指向Person对象的共享智能指针,接下来这三条语句,peoeple中的第一个元素的m_partner指向了第二个元素中的Person对象,第二个元素的m_partner指向了第三个元素中Person的对象,第三个元素的m_partner指向了第一个元素中的Person对象。这样每个对象的引用计数都是2.当people向量离开作用域销毁后,会将每个对象的引用计数减1,但每个对象的成员m_partner仍存在,所以无法删除Person对象,最终导致内存的泄露。为了防止这种情况出现,就避免出现share_ptr循环引用情况。或者结合使用另外一种智能指针weak_ptr;例如将Person成员变量中的share_ptr改为weak_ptr,当peolple离开作用域销毁后,他所指向的对象也都自动销毁。

weak_ptr不能单独使用,需要结合share_ptr一起使用。我weak_ptr的对象可以将share_ptr作为构造函数的参数初始化,或者定义一个空的weak_ptr,然后将share_ptr对象赋值给它。如下代码

#include<memory>
#include<iostream>
#include<vector>
using namespace std;
class A {};
int main()
{shared_ptr<A> sp1 = make_shared<A>();weak_ptr<A>wp1(sp1);weak_ptr<A>wp2;wp2 =sp1;cout << "use count: " << wp2.use_count() << endl;}

打印结果

weak_ptr的主要特征是,只对share_ptr所管理的的对象进行观测,不会改变对象的引用计数。如下代码

#include<memory>
#include<iostream>
#include<vector>
using namespace std;
class Rectangle {public:Rectangle(double w, double h) :width(w), height(h) {}~Rectangle() {}double area(){return width * height;}
private:double width;double height;
};
int main()
{weak_ptr<Rectangle>w_sp1;{shared_ptr<Rectangle>sp1(new Rectangle(1, 2));shared_ptr<Rectangle>sp2 = sp1;w_sp1 = sp2;cout << "作用域内部usecount=" << w_sp1.use_count() << endl;}cout << "作用域外部usercount=" << w_sp1.use_count() << endl;cout << "expired=" << w_sp1.expired() << endl;//为1时所观测的对象不可用
}

打印结果

我们可以通过weak_ptr使用lock函数来获得一个shared_ptr以获得封装对象的控制权。在下面这个例子里我们输出share_ptr里封装对象指针,由于share_ptr重载了插入运算符,所以可以直接打印出封装的指针的值。

#include<memory>
#include<iostream>
#include<vector>
using namespace std;
class Rectangle {public:Rectangle(double w, double h) :width(w), height(h) {}~Rectangle() {}double area(){return width * height;}
private:double width;double height;
};
int main()
{weak_ptr<Rectangle>w_sp1;{shared_ptr<Rectangle>sp1(new Rectangle(1, 2));shared_ptr<Rectangle>sp2 = sp1;w_sp1 = sp2;shared_ptr<Rectangle>sp3 = w_sp1.lock();cout << "作用域内部sp3=" << sp3 << endl;}shared_ptr<Rectangle>sp3 = w_sp1.lock();cout << "作用域外部sp3=" << sp3 << endl;cout << "expired=" << w_sp1.expired() << endl;//为1时所观测的对象不可用
}

打印结果

可以看到在作用域里面share_ptr是个有效指针,而在作用域外是个空指针。所以当我们使用weak_ptr观察对象是否为空指针时,并不需要使用use_count或expired来判断 ,而是可以直接调用lock函数获得一个share_ptr对象,如果share_ptr包含的指针非空那么就可用。由于lock对象是个原子操作,是一个不可分割的操作,当原子操作执行进程中时,其它线程不能读取,修改或者中断操作的数据,因此,使用lock函数保证了多线程时获得的share_ptr中的对象是安全有效的,此外,在多线程环境下,当使用expired函数时,只有当expired返回true时才是有意义的。

下面时weak_ptr和share_ptr直接的关系。

http://www.dtcms.com/wzjs/40498.html

相关文章:

  • 网站建设 前沿文章网站制作公司有哪些
  • 网站建设征求意见分析报告google搜索引擎入口
  • 腾讯网站站内面包屑导航网站收录情况查询
  • 动态网站 费用珠海百度关键词优化
  • seo营销网站的设计标准直销的八大课程
  • 一建建设网站首页成都今天重大新闻事件
  • 普通网站可以做商城电商大数据查询平台
  • 品牌网站建设绿d茶760关键词排名查询
  • 网站用户注册怎么做免费的推广网站
  • 彩票走势网站怎么做的怎么申请一个网站
  • 网站开发维护多少钱网络营销的基本方法
  • 网站建设好怎么发布东莞网络推广营销
  • dede微电影网站模板精准客源引流平台
  • 山东建设厅执业资格注册中心网站长沙网站优化
  • asp.net mvc5网站开发成都网站seo推广
  • 张家港做网站海外互联网推广平台
  • 西安高风险调整seo标题优化关键词怎么选
  • 网上北京网站制作公司产品软文模板
  • 网站建设重庆网站流量来源
  • 扁平化设计网站建设合肥seo推广排名
  • 企业形象网站用什么语言开发产品营销网站建设
  • 欧美网站建设排名大全百度站长平台官网登录入口
  • 做家教的正规网站b2b多平台一键发布
  • 网站被k是怎么回事google推广教程
  • 做视频背景音乐网站2023年国家免费技能培训
  • 高端网站建设公司服务好吗seo的定义
  • 网站建设改版目的西安网络推广运营公司
  • 重庆网站建设选承越磁力链 ciliba
  • 一般做网站是在什么网站找素材网站快速排名
  • 专题活动是在官方网站还是在其他网站做实时疫情最新消息数据