Boost.smart_ptr.shared_ptr--2
来源:互联网 发布:uefi双硬盘安装ubuntu 编辑:程序博客网 时间:2024/06/05 06:06
// shared_ptr.cpp : 定义控制台应用程序的入口点。
//
#include "stdafx.h"
#include <iostream>
#include <boost\smart_ptr\shared_ptr.hpp> //shared_ptr类文件
#include <boost\make_shared.hpp> //make_shared类文件
#include <string>
using namespace boost;
using namespace std;
/********************************************************************
*shared_ptr类指针知识点:
(1)shared_ptr智能指针是最接近原指针的,它具有了原指针的一切操作,应用范围最广;
(2)用法:
①shared_ptr<T> sp(new T());
②可以赋值,比较操作指针;
*shared_ptr类成员函数
(1)reset函数:作用将引用当前指针的计数减1,停止对指针的共享;如果计数不为0,不删除;
(2)unique函数用来检查指针是否是唯一的,当唯一的时候,返回true;
(3)重载了比较赋值等运算
(4)可以用到容器中,可以使用容器作为元素;可以作为容器的元素,这个重点,容器中存放的是指针,可以理解为指针数组;
(5) use_count统计了当前的引用计数
********************************************************************/
class Test
{
public:
Test(string a)
{
this->a=a;
}
string a;
~Test()
{
cout<<a<<" Test over"<<endl;
}
};
int _tmain(int argc, _TCHAR* argv[])
{
boost::shared_ptr<Test> spTest1(new Test("Test1")); //指针对象1
cout <<"spTest1 count:"<<spTest1.use_count()<<spTest1->a<<endl;
boost::shared_ptr<Test> spTest2(spTest1);
boost::shared_ptr<Test> spTest3(spTest1);
cout <<"spTest1 count:"<<spTest1.use_count()<<spTest1->a<<endl;
spTest3->a="modofy";
cout <<"spTest1 count:"<<spTest1.use_count()<<spTest1->a<<endl;
cout<<"*******************************************************"<<endl;
boost::shared_ptr<Test> spTest4(boost::make_shared<Test>("Test4"));//指针对象2,使用工厂函数类
spTest3=spTest4;
cout <<"spTest1 count:"<<spTest1.use_count()<<spTest1->a<<endl;
cout <<"spTest3 count:"<<spTest4.use_count()<<spTest4->a<<endl;
cout <<"spTest4 count:"<<spTest4.use_count()<<spTest4->a<<endl;
return 0;
//
#include "stdafx.h"
#include <iostream>
#include <boost\smart_ptr\shared_ptr.hpp> //shared_ptr类文件
#include <boost\make_shared.hpp> //make_shared类文件
#include <string>
using namespace boost;
using namespace std;
/********************************************************************
*shared_ptr类指针知识点:
(1)shared_ptr智能指针是最接近原指针的,它具有了原指针的一切操作,应用范围最广;
(2)用法:
①shared_ptr<T> sp(new T());
②可以赋值,比较操作指针;
*shared_ptr类成员函数
(1)reset函数:作用将引用当前指针的计数减1,停止对指针的共享;如果计数不为0,不删除;
(2)unique函数用来检查指针是否是唯一的,当唯一的时候,返回true;
(3)重载了比较赋值等运算
(4)可以用到容器中,可以使用容器作为元素;可以作为容器的元素,这个重点,容器中存放的是指针,可以理解为指针数组;
(5) use_count统计了当前的引用计数
********************************************************************/
class Test
{
public:
Test(string a)
{
this->a=a;
}
string a;
~Test()
{
cout<<a<<" Test over"<<endl;
}
};
int _tmain(int argc, _TCHAR* argv[])
{
boost::shared_ptr<Test> spTest1(new Test("Test1")); //指针对象1
cout <<"spTest1 count:"<<spTest1.use_count()<<spTest1->a<<endl;
boost::shared_ptr<Test> spTest2(spTest1);
boost::shared_ptr<Test> spTest3(spTest1);
cout <<"spTest1 count:"<<spTest1.use_count()<<spTest1->a<<endl;
spTest3->a="modofy";
cout <<"spTest1 count:"<<spTest1.use_count()<<spTest1->a<<endl;
cout<<"*******************************************************"<<endl;
boost::shared_ptr<Test> spTest4(boost::make_shared<Test>("Test4"));//指针对象2,使用工厂函数类
spTest3=spTest4;
cout <<"spTest1 count:"<<spTest1.use_count()<<spTest1->a<<endl;
cout <<"spTest3 count:"<<spTest4.use_count()<<spTest4->a<<endl;
cout <<"spTest4 count:"<<spTest4.use_count()<<spTest4->a<<endl;
return 0;
}
可以看到结果,shared_ptr可以像裸指针那样任意的操作,并且保证了安全,指针被引用一次就计数加1,反之则减1;
- Boost.smart_ptr.shared_ptr--2
- 智能指针Boost.smart_ptr学习4--shared_ptr
- Boost Smart_ptr
- Boost::smart_ptr
- boost::smart_ptr
- smart_ptr之shared_ptr
- smart_ptr之shared_ptr(二)
- 智能指针Boost.smart_ptr学习2--scoped_ptr
- 在smart_ptr中处理引用计数时使用swap概念(boost::shared_ptr.hpp)
- Boost库学习随记三 smart_ptr库、soped_ptr、scoped_array、shared_ptr、shared_array、weak_ptr等等
- Boost.smart_ptr.scoped_ptr--1
- Boost之Smart_ptr库
- boost.smart_ptr库简介
- <Boost> smart_ptr智能指针
- Boost.Smart_ptr/enable_share_from_this笔记
- boost smart_ptr -> scoped_ptr
- boost smart_ptr 3-> scoped_array
- Boost.smart_ptr智能指针
- 使用Qt实现简单的图片预览效果
- Spring MVC "The request sent by the client was syntactically incorrect ()"解决办法
- jsp面试基础速成精华讲解
- Zend Framework 入门——错误处理
- java JList 添加数据后,如何更新啊
- Boost.smart_ptr.shared_ptr--2
- Delphi 中的自定义类笔记
- 经典Sql面试题收集!!
- 文件选择对话框filedialog,OpenFileDialog,FolderBrowserDialog
- 关于Excel导入的问题记录
- Java 中的堆和栈
- 嵌套For循环性能优化案例
- jsp生成的java在work目录下
- 移动项目必知必会——打包ipa时的真机尺寸