#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. IT基础架构

  2. Django hrf:权限、频率控制

    一.权限 二.频率控制 一.权限 1.权限介绍 只有超级用户才能访问指定的数据,所以就要用权限组件进行设置 2.局部使用 # 单独抽出写一个视图类 from rest_framework.permis ...

  3. 安装mysql 好不容易成功了 却连不上

    [mysqld] skip-grant-tables datadir=/var/lib/mysql socket=/var/lib/mysql/mysql.sock # Disabling symbo ...

  4. 查看python和NumPy版本和安装路径

    记录查看Python和NumPy版本以及路径的几条命令 # 查看Python版本及路径 python -V python -c "import sys;print(sys.executabl ...

  5. less网站

    less中文网站:http://lesscss.cn/ 旧版的:http://www.bootcss.com/p/lesscss/

  6. GDI+图像编程

    一.Graphics GDI+是GDI(Windows Graphics Device Interface)的后继者,它是.NET Framework为操作图形提供的应用程序编程接口,主要用在窗体上绘 ...

  7. 一篇文章教你如何部署.NET Core WPF应用,你还在等什么?

    DevExpress广泛应用于ECM企业内容管理. 成本管控.进程监督.生产调度,在企业/政务信息化管理中占据一席重要之地.通过DevExpress WPF Controls,您能创建有着强大互动功能 ...

  8. 12、生命周期-@Bean指定初始化和销毁方法

    12.生命周期-@Bean指定初始化和销毁方法 Bean的生命周期:创建->初始化->销毁 容器管理bean的生命周期 我们可以自定义初始方法和销毁方法,容器在bean进行到当期那生命周期 ...

  9. 7、Spring Boot 2.x 集成 Redis

    1.7 Spring Boot 2.x 集成 Redis 简介 继续上篇的MyBatis操作,详细介绍在Spring Boot中使用RedisCacheManager作为缓存管理器,集成业务于一体. ...

  10. 从入门到精通djang Django

    http://docs.30c.org/djangobook2/ 推荐大家一本书 特别用用  中文版的 哦