追加一个shared_ptr指针

#include <memory>

using namespace std;

int _tmain(int argc, _TCHAR* argv[])
{
//===========================
// 错误 两个只能指针 管理一个已经分配的内存
//int* p = new int(7);
//shared_ptr<int> p1(p);
//shared_ptr<int> p2(p);
//============================ //================================
// 正确做法
shared_ptr<int> sp1(new int());
shared_ptr<int> sp2(sp1); //
//================================ return ;
}

weak_ptr示例

/*
// 使用shred_ptr的主要原因就是避免关注指针指向的资源
// 只能指针将自动释放与不再需要的对象的相关资源
// 但是某些情况下,这种却不是我们需要的。
// 比如 循环引用.两个对象都引用对方。
// 又或者 分享一个对象 但是不占有该对象
// 这个时候可以使用weak_ptr 其实相对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<shared_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 _tmain(int argc, _TCHAR* argv[])
{
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 nico's mom: "
<< p->mother->kids[]->name << endl; p = initFamily("jim");
cout << "jim's family exists" << endl; return ;
}

运行代码结果如下:

nico's family exists
- nico is shared 3 times
- name of 1st kid of nico's mom: nico
jim's family exists
请按任意键继续. . .

实际上我们并没有看到 指针指向资源的释放 这是因为在class Person 中的vector中 使用了shared_ptr 指针增加了资源的计数 所以没有及时释放

我们应该将vector这个容器改为weak_ptr  同时在使用上 weak_ptr 需要使用lock() 函数将其提升为shared_ptr

/*
// 使用shred_ptr的主要原因就是避免关注指针指向的资源
// 只能指针将自动释放与不再需要的对象的相关资源
// 但是某些情况下,这种却不是我们需要的。
// 比如 循环引用.两个对象都引用对方。
// 又或者 分享一个对象 但是不占有该对象
// 这个时候可以使用weak_ptr 其实相对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; //weakpointer!!!
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 _tmain(int argc, _TCHAR* argv[])
{
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 nico's mom: "
<< p->mother->kids[].lock()->name << endl; p = initFamily("jim");
cout << "jim's family exists" << endl; return ;
}

运行结果如下

nico's family exists
- nico is shared 1 times
- name of 1st kid of nico's mom: nico
delete nico
delete nico's dad
delete nico's mom
jim's family exists
delete jim
delete jim's dad
delete jim's mom
请按任意键继续. . .

可以看到 资源正确释放

代码 改编自 C++标准库——自学教程与参考手册 英文第二版

c++智能指针(2)的更多相关文章

  1. enote笔记法使用范例(2)——指针(1)智能指针

    要知道什么是智能指针,首先了解什么称为 “资源分配即初始化” what RAII:RAII—Resource Acquisition Is Initialization,即“资源分配即初始化” 在&l ...

  2. C++11 shared_ptr 智能指针 的使用,避免内存泄露

    多线程程序经常会遇到在某个线程A创建了一个对象,这个对象需要在线程B使用, 在没有shared_ptr时,因为线程A,B结束时间不确定,即在A或B线程先释放这个对象都有可能造成另一个线程崩溃, 所以为 ...

  3. C++智能指针

    引用计数技术及智能指针的简单实现 基础对象类 class Point { public: Point(int xVal = 0, int yVal = 0) : x(xVal), y(yVal) { ...

  4. EC笔记:第三部分:17、使用独立的语句将newed对象放入智能指针

    一般的智能指针都是通过一个普通指针来初始化,所以很容易写出以下的代码: #include <iostream> using namespace std; int func1(){ //返回 ...

  5. 智能指针shared_ptr的用法

    为了解决C++内存泄漏的问题,C++11引入了智能指针(Smart Pointer). 智能指针的原理是,接受一个申请好的内存地址,构造一个保存在栈上的智能指针对象,当程序退出栈的作用域范围后,由于栈 ...

  6. 智能指针unique_ptr的用法

    unique_ptr是独占型的智能指针,它不允许其他的智能指针共享其内部的指针,不允许通过赋值将一个unique_ptr赋值给另一个unique_ptr,如下面错误用法: std::unique_pt ...

  7. 基于C/S架构的3D对战网络游戏C++框架 _05搭建系统开发环境与Boost智能指针、内存池初步了解

    本系列博客主要是以对战游戏为背景介绍3D对战网络游戏常用的开发技术以及C++高级编程技巧,有了这些知识,就可以开发出中小型游戏项目或3D工业仿真项目. 笔者将分为以下三个部分向大家介绍(每日更新): ...

  8. C++ 引用计数技术及智能指针的简单实现

    一直以来都对智能指针一知半解,看C++Primer中也讲的不够清晰明白(大概是我功力不够吧).最近花了点时间认真看了智能指针,特地来写这篇文章. 1.智能指针是什么 简单来说,智能指针是一个类,它对普 ...

  9. C++11智能指针读书笔记;

    智能指针是一个类对象,而非一个指针对象. 原始指针:通过new建立的*指针 智能指针:通过智能指针关键字(unique_ptr, shared_ptr ,weak_ptr)建立的指针 它的一种通用实现 ...

  10. 「C++」理解智能指针

    维基百科上面对于「智能指针」是这样描述的: 智能指针(英语:Smart pointer)是一种抽象的数据类型.在程序设计中,它通常是经由类型模板(class template)来实做,借由模板(tem ...

随机推荐

  1. 修改 Tomcat Connector运行模式 优化Tomcat运行性能

    omcat是一个小型的轻量级应用服务器,也是JavaEE开发人员最常用的服务器之一.不过,许多开发人员不知道的是,Tomcat Connector(Tomcat连接器)有bio.nio.apr三种运行 ...

  2. svn git 必须理解的概念

    不都是SCM代码管理嘛,有很大区别么?很多svn老鸟都是抱着这样的心态去学习git,然后无一幸免地陷入“查阅过很多资料,依然掌握不好”的困境,至少我们团队是这样的. 网上的资料确实已经很多了,却没有把 ...

  3. Java中构造函数传参数在基本数据类型和引用类型之间的区别

    Java中构造函数传参数在基本数据类型和引用类型的区别 如果构造函数中穿的参数为基本数据类型,如果在函数中没有返回值,在调用的时候不会发生改变:而如果是引用类型,改变的是存储的位置,所有不管有没有返回 ...

  4. 外购半成品报SHORT问题(非验货客户)

    外购半成品报SHORT问题(验货客户)https://www.cnblogs.com/Snowfun/p/8660646.html 下面看非验货客户: 1.检查采购类型是否为F(SAP_MARC),为 ...

  5. w3af安装

    基于python开发,由于kali自带的w3af无法正常使用 安装新的w3af apt-get update apt-get install kali-linux-all安装所有工具包 apt-get ...

  6. 修改.net反编译的dll

    用.Net reflector 打开,配合reflexil工具. 有两种修改方法. 1.重写,试过,但不好用. 2.修改IL指令 一般只需修改简单的if判断. 方法:找到需要修改的行,把brfalse ...

  7. java面试题:基础知识

    类和对象 Q:讲一下面向对象OOP思想. 面向对象主要是抽象,封装,继承,多态. 多态又分为重载和重写.重载主要是方法参数类型不一样,重写则是方法内容不一样. Q:抽象类和接口有什么区别? 抽象类中可 ...

  8. Android四大组件总结

    1:Actiivty 用户可以看见并可以操作的界面 Activity开启方式:startActivityForResult :startActivity Activity生命周期: onCreate  ...

  9. js对键盘输入事件绑定到特定按钮

    转自:https://www.cnblogs.com/liluping860122/archive/2013/05/25/3099103.html<script type="text/ ...

  10. 【转】关于easyui的窗口和tab页面不执行js说明

    原地址:http://www.jeasyuicn.com/post-49.html 一直以来群里里面很多人反应,在用tab加载界面的时候,界面里面的js不会执行.今天GodSon在此说明一下原因. 不 ...