#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的更多相关文章

  1. 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), ...

  2. C++ 智能指针 auto_ptr 和 shared_ptr

    首先,如果你不知道什么是智能指针,请先移步:C++智能指针简单剖析 1.auto_ptr #ifndef AUTO_PTR_H #define AUTO_PTR_H template<typen ...

  3. c++——智能指针学习(shared_ptr和weak_ptr)

    先看一个例子:Stark和Targaryen家族你中有我,我中有你.我们设计以下类企图避免内存泄漏,使得析构函数都能调用到: #include<iostream> #include< ...

  4. C++智能指针 auto_ptr、shared_ptr、weak_ptr和unique_ptr

    手写代码是理解C++的最好办法,以几个例子说明C++四个智能指针的用法,转载请注明出处. 一.auto_ptr auto_ptr这是C++98标准下的智能指针,现在常常已经被C++标准的其他智能指针取 ...

  5. 聊聊智能指针 auto_ptr、shared_ptr、weak_ptr和unique_ptr

    本文为转载:https://www.cnblogs.com/zeppelin5/p/10083597.html,对作者有些地方做了修正. 手写代码是理解C++的最好办法,以几个例子说明C++四个智能指 ...

  6. 【C++】智能指针简述(四):shared_ptr

    在开始本文内容之前,我们再来总结一下,前文内容: 1.智能指针采用RAII机制,在构造对象时进行资源的初始化,析构对象时进行资源的清理及汕尾. 2.auto_ptr防止拷贝后析构释放同一块内存,采用& ...

  7. 34.share_ptr智能指针共享内存,引用计数

    #include <iostream> #include <memory> #include <string> #include <vector> us ...

  8. 初次窥见智能指针auto_ptr和shared_ptr

    #include <memory>//shared_ptr要用的头文件 using namespace std; class A //测试auto_ptr和shared_ptr的delet ...

  9. 智能指针剖析(下)boost::shared_ptr&其他

    1. boost::shared_ptr 前面我已经讲解了两个比较简单的智能指针,它们都有各自的优缺点.由于 boost::scoped_ptr 独享所有权,当我们真真需要复制智能指针时,需求便满足不 ...

随机推荐

  1. CentOS7安装CDH 第二章:CentOS7各个软件安装和启动

    相关文章链接 CentOS7安装CDH 第一章:CentOS7系统安装 CentOS7安装CDH 第二章:CentOS7各个软件安装和启动 CentOS7安装CDH 第三章:CDH中的问题和解决方法 ...

  2. Matplotlib介绍

    目录 一.    Matplotlib介绍    1 二.    初级绘制    1 1.    绘图简介    1 2.    在上面的过程中,主要就是下面三个元素:    1 三.    2D各种 ...

  3. linux 基础12-程序与资源管理

    1. 基础概念 可执行的二进制文件就是程序 执行程序的时候因触发事件而获取的ID,称为PID 在登入并执行bash时,系统依据登录者的UID/GID给登录者一个PID/GPID/SID等 启动程序时, ...

  4. Python查看模块函数,查看函数方法的详细信息

    Python查看方法的详情 1.通用的帮助函数help() 使用help()函数来查看函数的帮助信息. 如: import requests help(requests) 会有类似如下输出: 2.查询 ...

  5. toolbox 中创建nginx服务器,使用localhost不能访问

    使用toolbox 工具使用docker创建nginx 容器,使用localhost不能访问? 使用docker run --rm -d --name dweb  -p 80:80 nginx 命令执 ...

  6. APP微信登录 服务器处理代码

    采用框架THINKPHP5 需要客户端传的参数有  udid openid nickname avatar_path /* * @param 第三方微信登录 * @param openid udid ...

  7. 基于JQ的记忆翻牌游戏

    <!DOCTYPE html> <html lang="zh-CN"> <head> <meta charset="UTF-8& ...

  8. 开发中少不了的Fun -- 前端本地存储

    存储sessionStorage function setSessionStore (name, content) { if (!name) return if (typeof content !== ...

  9. python 传参

    python不允许程序员选择采用传值还是传引用.Python参数传递采用的肯定是“传对象引用”的方式.这种方式相当于传值和传引用的一种综合.如果函数收到的是一个可变对象(比如字典或者列表)的引用,就能 ...

  10. Python中的memoryview

    Python中的memoryview提供了类似C语言指针的功能,有了memoryview,如果某个Object支持buffer protocol,那么就可以通过memory去访问到他的内部. Pyth ...