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

主机搭建网站教程移动微网站建设

主机搭建网站教程,移动微网站建设,wordpress建站案例,wordpress批量发布文章文章目录 1. 计数指针 shared_ptr2. shared_ptr 与函数3. shared_ptr 与 unique_ptr 1. 计数指针 shared_ptr shared_ptr 计数指针又称共享指针,是C11引入的智能指针之一。与unique_ptr不同的是,shared_ptr允许多个指针共享同一块内存。每当一个shared_…

文章目录

  • 1. 计数指针 shared_ptr
  • 2. shared_ptr 与函数
  • 3. shared_ptr 与 unique_ptr

1. 计数指针 shared_ptr

shared_ptr 计数指针又称共享指针,是C++11引入的智能指针之一。与unique_ptr不同的是,shared_ptr允许多个指针共享同一块内存。每当一个shared_ptr 被赋值时,引用计数就会增加。当shraed_ptr被销毁时,引用计数会减小。当引用计数为0时,内存会被自动释放。

shared_ptr内部维护着一个引用计数器,可以通过 use_count() 获取当前引用计数。

代码示例:

#include <iostream>
#include <memory>
using namespace std;int main(int argc, char *argv[]){std::shared_ptr<int> i_p_1 = make_shared<int>(10);cout<< "value: "<< *i_p_1 <<endl;cout<< "use count: "<<i_p_1.use_count() <<endl;// Copystd::shared_ptr<int> i_p_2 = i_p_1;cout<< "value: "<< *i_p_2<<endl;cout<< "i_p_1 use count: " << i_p_1.use_count() << endl;cout<< "i_p_2 use count: " << i_p_2.use_count() << endl;// 修改i_p_2的值*i_p_2 = 30;cout<< "i_p_1 value: " << *i_p_1 << endl;cout<< "i_p_2 value: " << *i_p_2 << endl;// i_p_1 设置为 nullptrstd::shared_ptr<int> i_p_3 = i_p_1;// i_p_1 = nullptr;i_p_1.reset();cout<< "i_p_1 use count: " << i_p_1.use_count() << endl;cout<< "i_p_2 use count: " << i_p_2.use_count() << endl;cout<< "i_p_3 use count: " << i_p_3.use_count() << endl;return 0;
}

代码解析:

  • i_p_1 被初始化为一个 shared_ptr,指向整数10
  • i_p_2 是 i_p_1的副本,因此引用计数会增加
  • 修改了 i_p_2 的值,i_p_1也会变,因为他们共享一块内存
  • 当 i_p_1 被reset() 时,引用计数减少,最终,所有shared_ptr 被销毁时,内存会被自动释放。

2. shared_ptr 与函数

值传入
当 shared_ptr 作为函数参数传递时,默认是值传递(复制传递)。这会增加引用计数,确保指针在函数内部被正确管理。

#include <iostream>
#include <memory>
#include "cat.h"
using namespace std;// 值传入
void cat_by_value(std::shared_ptr<Cat> cat){cout<< cat->get_name() <<endl;cat->set_cat_name("ee");cout<< "func use count :" << cat.use_count() << endl; // 2
}// 引用传入
void cat_by_ref(std::shared_ptr<Cat> &cat){cout<< cat->get_name() <<endl;cat->set_cat_name("ff");cout<< "func use count :" << cat.use_count() << endl;
}// 作为返回值
std::shared_ptr<Cat> get_shared_ptr(){std::shared_ptr<Cat> cat_p = std::make_shared<Cat>("local cat");return cat_p;
}int main(int argc, char *argv[]){std::shared_ptr<Cat> c1 = make_shared<Cat>("dd");cat_by_value(c1);    // 值传递cat_by_ref(c1);         // 引用传递c1->cat_info();cout<< "c1 use_count : " << c1.use_count()<<endl; // 返回值get_shared_ptr()->cat_info();return 0;
}

代码解析:

  • 值传递:当 shared_ptr 作为参数传递时,引用计数增加。函数结束时,局部 shared_ptr 会被销毁,引用计数会减少。
  • 引用传递:使用引用传递时,传递的是原始 shared_ptr,不会增加引用计数
  • 返回值:当 shared_ptr 作为返回值返回时,shared_ptr 会被复制到调用者

3. shared_ptr 与 unique_ptr

  • 不能将 shared_ptr 转化为 unique_ptr
  • unique_ptr 可以转化为 shared_ptr
    • 通过 std::move

常见的设计:

  • 将你的函数返回 unique_ptr 是一种常见的设计模式,这样可以提高代码的复用度,你可以随时改变为 shared_ptr

代码示例:

#include <iostream>
#include <memory>
#include "cat.h"
using namespace std;std::unique_ptr<Cat> get_unique_ptr(){std::unique_ptr<Cat> cat_p = std::make_unique<Cat>("local cat");return cat_p;
}int main(int argc, char *argv[]){std::unique_ptr<Cat> c_p_1 = std::make_unique<Cat>("dd");std::shared_ptr<Cat> c_p_2 = std::move(c_p_1);cout<< "c_p_2.use_count: " <<c_p_2.use_count()<<endl;std::shared_ptr<Cat> c_p_3 = get_unique_ptr(); if(c_p_3){c_p_3->cat_info();cout<< "c_p_3.use_count: "<<c_p_3.use_count()<<endl;}return 0;
}

代码解析:

  • std::move 转换:c_p_1 是一个 unique_ptr,通过 std::move 将其所有权转移给 c_p_2,并转换为 shared_ptr。
  • unique_ptr 到 shared_ptr:返回的 unique_ptr 可以通过 std::move 转换为 shared_ptr,使得多个指针能够共享这块内存
http://www.dtcms.com/a/501256.html

相关文章:

  • 成都网站建设加推广公司东阿网站建设产品
  • 怎么提升搜狗网站排名网络编程技术栈
  • 沈阳中联做网站注册网站英语怎么说
  • 网站服务器 英文建设厅网站平顶山
  • 成都网站品牌设计案例打开一个网站
  • 百度基木鱼建站济南高新区 网站建设公司
  • 网站策划书的编写做旅游那些网站好
  • 邹城市住房和建设局网站共享空间网站开发公司
  • aaaa景区网站建设标准托管竞价推广公司
  • 南昌网站设计案例wordpress 编辑
  • 做教育机构的设计哪些网站好网站建设实训报告2000字
  • seo做的不好的网站正规app软件开发报价
  • 代做网站转账截图网站建设毕业设计刻光盘
  • 政务网站开发理念用宝塔做网站步骤
  • 自己的电脑做服务器,并建网站wordpress 图片 模糊
  • 有关网站开发的文献国外推广渠道有哪些方式
  • 网站布局设计软件模板商城建站
  • 石家庄网站建设公司排名手机兼职可以做什么
  • 个别网站网速慢怎么做可以进行网站外链建设的有
  • 建网站公司用什么网站程序网站备案时核验单
  • 手机网站建站公司做网站需要字体授权
  • 货代网站制作邯郸网站优化公司
  • 江门外贸网站建设信阳网络推广公司
  • omp_get_thread_num为0,真是奇怪了。
  • 厦门的服装商城网站建设足球比赛直播在线观看
  • 网站运营作用哈尔滨百度网站建设
  • 河北建设厅网站没有注册广州seo工程师
  • 佛山做公司网站蓝翔老师做的网站
  • 做外贸平台还是网站网站运营主管是干什么的
  • 反馈网站怎么做wordpress5.2.2编辑器中文