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

重庆seo网站推广工具济南网页设计师招聘信息

重庆seo网站推广工具,济南网页设计师招聘信息,年终总结ppt模板免费下载网站,世界排名前十位shared_ptr使用的注意事项: 1.不能使用一个原始地址初始化多个共享智能指针 2.函数不能返回管理了this的共享智能指针对象 3.共享智能指针不能循环引用 不能使用一个原始地址初始化多个共享智能指针 代码如下: #include iostream #include memory using name…shared_ptr使用的注意事项: 1.不能使用一个原始地址初始化多个共享智能指针 2.函数不能返回管理了this的共享智能指针对象 3.共享智能指针不能循环引用 不能使用一个原始地址初始化多个共享智能指针 代码如下: #include iostream #include memory using namespace std;//shared_ptr使用的注意事项: //1.不能使用一个原始地址初始化多个共享智能指针 //2.函数不能返回管理了this的共享智能指针对象 //3.共享智能指针不能循环引用struct Test {shared_ptrTest getSharedPtr(){return shared_ptrTest(this);}~Test(){cout class Test is disstruct ... endl;} };int main() {Test *t new Test;shared_ptrTest ptr1(t);shared_ptrTest ptr2(t);return 0; } 测试结果: 这块堆内存被析构了两次所以出现了错误。 正确写法: int main() {Test *t new Test;shared_ptrTest ptr1(t);shared_ptrTest ptr2 ptr1;return 0; } 测试结果: 函数不能返回管理了this的共享智能指针对象 代码如下: #include iostream #include memory using namespace std;//shared_ptr使用的注意事项: //1.不能使用一个原始地址初始化多个共享智能指针 //2.函数不能返回管理了this的共享智能指针对象 //3.共享智能指针不能循环引用struct Test {shared_ptrTest getSharedPtr(){return shared_ptrTest(this);}~Test(){cout class Test is disstruct ... endl;} };int main() {shared_ptrTest sp1(new Test);cout use_count sp1.use_count() endl;shared_ptrTest sp2 sp1-getSharedPtr();cout use_count sp1.use_count() endl;return 0; } 测试结果: 通过输出的结果可以看到一个对象被析构了两次其原因是这样的在这个例子中使用同一个指针 this 构造了两个智能指针对象 sp1 和 sp2这二者之间是没有任何关系的因为 sp2 并不是通过 sp1 初始化得到的实例对象。在离开作用域之后 this 将被构造的两个智能指针各自析构导致重复析构的错误。 这个问题可以通过 weak_ptr 来解决通过 wek_ptr 返回管理 this 资源的共享智能指针对象 shared_ptr。C11 中为我们提供了一个模板类叫做 std::enable_shared_from_this这个类中有一个方法叫做 shared_from_this()通过这个方法可以返回一个共享智能指针在函数的内部就是使用 weak_ptr 来监测 this 对象并通过调用 weak_ptr 的 lock() 方法返回一个 shared_ptr 对象。 使用weak_ptr解决shared_ptr管理的内存被重复析构的问题 代码如下: #include iostream #include memory using namespace std;//shared_ptr使用的注意事项: //1.不能使用一个原始地址初始化多个共享智能指针 //2.函数不能返回管理了this的共享智能指针对象 //3.共享智能指针不能循环引用struct Test:public enable_shared_from_thisTest {shared_ptrTest getSharedPtr(){return shared_from_this();}~Test(){cout class Test is disstruct ... endl;} };int main() {shared_ptrTest sp1(new Test);cout use_count sp1.use_count() endl;shared_ptrTest sp2 sp1-getSharedPtr();cout use_count sp1.use_count() endl;return 0; } 测试结果: 共享智能指针不能循环引用 代码如下: #include iostream #include memory using namespace std;struct TA; struct TB;struct TA {shared_ptrTB bptr;~TA(){cout class TA is disstruct... endl;} };struct TB {shared_ptrTA aptr;~TB(){cout class TB is disstruct endl;} };void test() {shared_ptrTA ap(new TA);shared_ptrTB bp(new TB);cout TA use_count ap.use_count() endl;cout TB use_count bp.use_count() endl;ap-bptr bp;bp-aptr ap;cout TA use_count ap.use_count() endl;cout TB use_count bp.use_count() endl;}int main() {test();return 0; }测试结果: 在测试程序中共享智能指针 ap、bp 对 TA、TB 实例对象的引用计数变为 2在共享智能指针离开作用域之后引用计数只能减为1这种情况下不会去删除智能指针管理的内存导致类 TA、TB 的实例对象不能被析构最终造成内存泄露。通过使用 weak_ptr 可以解决这个问题只要将类 TA 或者 TB 的任意一个成员改为 weak_ptr修改之后的代码如下 #include iostream #include memory using namespace std;struct TA; struct TB;struct TA {weak_ptrTB bptr;~TA(){cout class TA is disstruct... endl;} };struct TB {shared_ptrTA aptr;~TB(){cout class TB is disstruct endl;} };void test() {shared_ptrTA ap(new TA);shared_ptrTB bp(new TB);cout TA use_count ap.use_count() endl;cout TB use_count bp.use_count() endl;ap-bptr bp;bp-aptr ap;cout TA use_count ap.use_count() endl;cout TB use_count bp.use_count() endl;}int main() {test();return 0; }测试结果: 通过输出的结果可以看到类 TA 或者 TB 的对象被成功析构了。 上面程序中在对类 TA 成员赋值时 ap-bptr bp; 由于 bptr 是 weak_ptr 类型这个赋值操作并不会增加引用计数所以 bp 的引用计数仍然为 1在离开作用域之后 bp 的引用计数减为 0类 TB 的实例对象被析构。 在类 TB 的实例对象被析构的时候内部的 aptr 也被析构其对 TA 对象的管理解除内存的引用计数减为 1当共享智能指针 ap 离开作用域之后对 TA 对象的管理也解除了内存的引用计数减为 0类 TA 的实例对象被析构。
http://www.pierceye.com/news/413429/

相关文章:

  • .net域名可以做银行网站吗做网站用模版
  • 嘉兴市平湖市建设局网站品牌设计公司 知乎
  • jfinal网站开发模板app开发网站
  • 成都和奇乐网站建设公司怎么样研发网站要多久
  • 蓬莱做网站北京宣传片
  • 网站建设 部署与发布wordpress多说插件
  • 池州做网站的公司哪里有网站开发技术
  • 网站建设内容策划外贸软件排行榜前十名
  • 微信官方网站公众平台郸城建设银行网站
  • .net 微信网站开发免费网站建设制作
  • 做网站需要啥备案之类的嘛传统的网站开发模式
  • 杭州网站seo优化最适合女生的专业排名
  • 广州市酒店网站设计交易平台网站怎么做
  • 江苏省示范校建设专题网站网站网页制作公司网站
  • 前海艾爻网站 建设磐安住房和城乡建设部网站
  • 网站程序h5电商seo是什么意思啊
  • 网站赚钱做跨境电商要什么费用
  • wordpress修改文件简单的seo网站优化排名
  • 专业网专业网站建设展示网站建设的ppt
  • 江淮网站开发商城网站 html模板
  • 上海网站制作电话淄博免费网站建设
  • 做动态在网站需要学什么宁波网站建设用什么软件
  • 靖江 建设局网站wordpress小工具缓存
  • 搜索网站的软件郑州企业展厅设计公司
  • 上海建设局官方网站做外包网站的公司是怎样的
  • 网站开发ppt方案模板wordpress如何导出数据字典
  • 网站加上视频对seo影响wordpress打开xml-rpc
  • 个人网站建设分几个步走单页面网站多少钱
  • 自己做网站详细步骤保定网站建设方案优化
  • 传奇手游网站大全9377公司网站建设安全的风险