智能指针-共享式shared_ptr
#include <iostream>
#include <string>
#include <vector>
#include <memory>
using namespace std;
class Person
{
public:
string name;
shared_ptr<Person> mother;
shared_ptr<Person> father;
vector<weak_ptr<Person>> kids;
Person(const string& n,
shared_ptr<Person> m=nullptr,
shared_ptr<Person> f = nullptr)
:name(n),mother(m),father(f){}
~Person(){
cout<<"delete"<<name<<endl;
}
};
shared_ptr<Person> initFamily(const string& name)
{
shared_ptr<Person> mom(new Person(name+"'s mom"));
shared_ptr<Person> dad(new Person(name+"'s dad"));
shared_ptr<Person> kid(new Person(name,mom,dad));
mom->kids.push_back(kid);
dad->kids.push_back(kid);
return kid;
}
int main()
{
shared_ptr<Person> p = initFamily("nico");
cout<<"nico's family exists"<<endl;
cout<<"- nico is shared"<<p.use_count<<" times"<<endl;
cout<<"-name of 1st kid of kico's mom:"<<p->mother->kids[0]->name<<endl;
p->initFamily("jim");
cout<<"jim's family exists..."<<endl;
return 0;
}
智能指针-共享式shared_ptr的更多相关文章
- c++11 智能指针 unique_ptr、shared_ptr与weak_ptr
c++11 智能指针 unique_ptr.shared_ptr与weak_ptr C++11中有unique_ptr.shared_ptr与weak_ptr等智能指针(smart pointer), ...
- C++ 智能指针 auto_ptr 和 shared_ptr
首先,如果你不知道什么是智能指针,请先移步:C++智能指针简单剖析 1.auto_ptr #ifndef AUTO_PTR_H #define AUTO_PTR_H template<typen ...
- c++——智能指针学习(shared_ptr和weak_ptr)
先看一个例子:Stark和Targaryen家族你中有我,我中有你.我们设计以下类企图避免内存泄漏,使得析构函数都能调用到: #include<iostream> #include< ...
- C++智能指针 auto_ptr、shared_ptr、weak_ptr和unique_ptr
手写代码是理解C++的最好办法,以几个例子说明C++四个智能指针的用法,转载请注明出处. 一.auto_ptr auto_ptr这是C++98标准下的智能指针,现在常常已经被C++标准的其他智能指针取 ...
- 聊聊智能指针 auto_ptr、shared_ptr、weak_ptr和unique_ptr
本文为转载:https://www.cnblogs.com/zeppelin5/p/10083597.html,对作者有些地方做了修正. 手写代码是理解C++的最好办法,以几个例子说明C++四个智能指 ...
- 【C++】智能指针简述(四):shared_ptr
在开始本文内容之前,我们再来总结一下,前文内容: 1.智能指针采用RAII机制,在构造对象时进行资源的初始化,析构对象时进行资源的清理及汕尾. 2.auto_ptr防止拷贝后析构释放同一块内存,采用& ...
- 34.share_ptr智能指针共享内存,引用计数
#include <iostream> #include <memory> #include <string> #include <vector> us ...
- 初次窥见智能指针auto_ptr和shared_ptr
#include <memory>//shared_ptr要用的头文件 using namespace std; class A //测试auto_ptr和shared_ptr的delet ...
- 智能指针剖析(下)boost::shared_ptr&其他
1. boost::shared_ptr 前面我已经讲解了两个比较简单的智能指针,它们都有各自的优缺点.由于 boost::scoped_ptr 独享所有权,当我们真真需要复制智能指针时,需求便满足不 ...
随机推荐
- 10_Azkaban案例实践3_Command操作HDFS
HDFS操作任务 1.创建job描述文件 # fs.job type=command command=/usr/local/src/hadoop-2.6.4/bin/hadoop fs -mkdir ...
- WdatePicker日期控件使用与值获取,以及选择日期完毕触发事件
踩过无数坑,调试了很长时间,写出适合自己需求的方法需求:没有查询按钮,要求选择日期后自动触发查询事件(只有日期选择完成后才触发), 解决方案:代码如下:检索开始时间: <input type=& ...
- java实战(一)-------jdk环境在windows安装及配置
1.jdk官方下载 http://www.oracle.com/technetwork/java/javase/downloads/index.html 点击下载windows的版本:jdk-13.0 ...
- 搭建一个jumpserver跳板机
1,部署jumpserver 建立阿里云公网源yum仓库 清除缓存重新建立缓存 ip后面直接回车,下面输入y 地址端口账户密码直接回车 ,询问跳过输入y,是否继续输入y 用户名回车,输入自己要设置的密 ...
- Pytest【定制fixture】
在pytest中的fixture是在测试函数运行前后,由pytest执行的外壳函数,fixture中的代码可以定制,满足多变的测试需求:包括定义传入测试中的数据集.配置测试前系统的初始化状态.为批量测 ...
- ZZNUOJ-2154:单身狗线下聚会【求N个数的最小公倍数,会超longlong,大数乘法,Java】
2154: 单身狗线下聚会 题目描述 马上就到七夕节了,单身狗们决定聚一聚.但是它们沉迷B站上的lo娘,他们每沉迷 ai 单身狗时间(这是它们专业计时)后就会休息 单身狗时间.它们想找到一个时间正好他 ...
- 洛谷1546 最短网络Agri-Net【最小生成树】【prim】
[内含最小生成树Prim模板] 题目:https://www.luogu.org/problemnew/show/P1546 题意:给定一个邻接矩阵.求最小生成树. 思路:点少边多用Prim. Pri ...
- 分布式文件系统FastDFS架构认知
FastDFS是一款类Google FS的开源分布式文件系统(应用级的分布式文件存储服务). FastDFS的设计理念 FastDFS是为互联网应用量身定做的分布式文件系统,充分考虑了冗余备份.负载均 ...
- Codeforces Round #585 (Div. 2) D. Ticket Game
链接: https://codeforces.com/contest/1215/problem/D 题意: Monocarp and Bicarp live in Berland, where eve ...
- csv和xlsx区别
CSV是文本文件,用记事本就能打开.XLS 是二进制的文件只有用 EXCEL 才能打开: CSV 文件格式只能保存活动工作表中的单元格所显示的文本和数值.数据列以逗号分隔,每一行数据都以回车符结束.如 ...