c++智能指针(2)
追加一个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)的更多相关文章
- enote笔记法使用范例(2)——指针(1)智能指针
要知道什么是智能指针,首先了解什么称为 “资源分配即初始化” what RAII:RAII—Resource Acquisition Is Initialization,即“资源分配即初始化” 在&l ...
- C++11 shared_ptr 智能指针 的使用,避免内存泄露
多线程程序经常会遇到在某个线程A创建了一个对象,这个对象需要在线程B使用, 在没有shared_ptr时,因为线程A,B结束时间不确定,即在A或B线程先释放这个对象都有可能造成另一个线程崩溃, 所以为 ...
- C++智能指针
引用计数技术及智能指针的简单实现 基础对象类 class Point { public: Point(int xVal = 0, int yVal = 0) : x(xVal), y(yVal) { ...
- EC笔记:第三部分:17、使用独立的语句将newed对象放入智能指针
一般的智能指针都是通过一个普通指针来初始化,所以很容易写出以下的代码: #include <iostream> using namespace std; int func1(){ //返回 ...
- 智能指针shared_ptr的用法
为了解决C++内存泄漏的问题,C++11引入了智能指针(Smart Pointer). 智能指针的原理是,接受一个申请好的内存地址,构造一个保存在栈上的智能指针对象,当程序退出栈的作用域范围后,由于栈 ...
- 智能指针unique_ptr的用法
unique_ptr是独占型的智能指针,它不允许其他的智能指针共享其内部的指针,不允许通过赋值将一个unique_ptr赋值给另一个unique_ptr,如下面错误用法: std::unique_pt ...
- 基于C/S架构的3D对战网络游戏C++框架_05搭建系统开发环境与Boost智能指针、内存池初步了解
本系列博客主要是以对战游戏为背景介绍3D对战网络游戏常用的开发技术以及C++高级编程技巧,有了这些知识,就可以开发出中小型游戏项目或3D工业仿真项目. 笔者将分为以下三个部分向大家介绍(每日更新): ...
- C++ 引用计数技术及智能指针的简单实现
一直以来都对智能指针一知半解,看C++Primer中也讲的不够清晰明白(大概是我功力不够吧).最近花了点时间认真看了智能指针,特地来写这篇文章. 1.智能指针是什么 简单来说,智能指针是一个类,它对普 ...
- C++11智能指针读书笔记;
智能指针是一个类对象,而非一个指针对象. 原始指针:通过new建立的*指针 智能指针:通过智能指针关键字(unique_ptr, shared_ptr ,weak_ptr)建立的指针 它的一种通用实现 ...
- 「C++」理解智能指针
维基百科上面对于「智能指针」是这样描述的: 智能指针(英语:Smart pointer)是一种抽象的数据类型.在程序设计中,它通常是经由类型模板(class template)来实做,借由模板(tem ...
随机推荐
- https方式下 git push 每次都要输入密码的解决办法
转载自:http://git.oschina.net/oschina/git-osc/issues/2586 作者:Zoker https方式每次都要输入密码,按照如下设置即可输入一次就不用再手输 ...
- PropertyGrid控件动态生成属性及下拉菜单 (转)
http://blog.sina.com.cn/s/blog_6f14b7010101b91b.html https://msdn.microsoft.com/zh-cn/library/ms1718 ...
- centos如何查看磁盘剩余空间
linux系统的Df命令是以磁盘分区为单位查看文件系统,可以加上参数查看磁盘剩余空间信息,命令格式: df -hl 显示格式为: 文件系统 容量 已用 可用 已用% 挂载点 /dev/hda5 487 ...
- as3.0控制声音大小
//加载声音 var sound:Sound=new Sound(); sound.load(new URLRequest("1.mp3")): //声明声道 var soundC ...
- python基础入门学习简单程序练习
1.简单的乘法程序 i = 256*256 print('The value of i is', i) 运行结果: The value of i is 65536 2.执行python脚本的两种方式 ...
- c++三种进制格式
来源:c++ primer plus 常用的进制有二进制,八进制,十进制,十六进制,在c++的头文件iostream里除了提供了endl控制符之外,还提供了控制进制的控制符,(不含二进制),分别是八进 ...
- PHPlaravel中从数据库中选择数据是增加时间条件及各种条件
注:附加条件后要加get函数. 1.public function getForDataTable($startTime,$endTime){ return $this->query() -&g ...
- SQL Server中row_number的用法
ROW_NUMBER()函数将针对SELECT语句返回的每一行,从1开始编号,赋予其连续的编号.在查询时应用了一个排序标准后,只有通过编号才能够保证其顺序是一致的,当使用ROW_NUMBER函数时,也 ...
- 模拟点击事件在alert前不起作用
本来想在ajax提交前点击一下模态框,直到返回处理之前都显示正在保存,发现如标题的现象,几经折腾没找到解决办法,发现可能是 alert线程阻塞(冒泡)引起的,也没找到解决办法,于是借助第三方插件lay ...
- Inno Setup自定义安装界面脚本
; 脚本由 Inno Setup 脚本向导 生成! ; 有关创建 Inno Setup 脚本文件的详细资料请查阅帮助文档! #define MyAppName "RemoteCard&quo ...