6.Boost之smartpointer



1自己实现一个智能指针的功能

#include
<iostream>

#include
<string>

#include
<vector>

#include
<algorithm>

#include
<functional>

#include
<stdlib.h>

 

using
namespace
std;

 

template<class
T>

class
pmy

{

public:

   
pmy()

   
{}

   
pmy(T
*t)

   
{

       
p =
t;

   
}

   
~pmy()

   
{

       
if (p
!= nullptr)

       
{

           
delete
p;

       
}

   
}

   
T operator *()

   
{

       
return *p;

   
}

 

private:

   
T *p
= nullptr;

};

 

class
Test

{

public:

   
Test()

   
{

       
cout <<
"Test 
create" <<
endl;

   
}

   
~Test()

   
{

       
cout <<
"Testdelete"<<
endl;

   
}

};

 

void
run()

{

   
pmy<Test>
p(new
Test);//智能指针,智能释放

   
//*p;

}

 

void
main()

{

   
run();

   
cin.get();

}

运行结果:

2.boost:scoped_ptr智能指针,独占内存,并且会自动释放内存,也就是说这片内存不用共享给别人用的时候可以用这个智能指针

#include
<iostream>

#include
<boost/scoped_ptr.hpp>

//#include<boost/scoped_array.hpp>

//#include<boost/shared_ptr.hpp>

//#include<boost/shared_array.hpp>

//#include<boost/weak_ptr.hpp>

//#include<windows.h>

 

using
namespace
std;

 

void
main()

{

   
//scoped_ptr(作用于指针,特点是独享)就是一个智能指针

   
//p用(new int)来初始化

   
boost::scoped_ptr<int>
p(new
int);//自动释放内存

   
*p = 12;

   
//get()获取指针指向的内容

   
cout << *p.get()
<< endl;

   
//reset()的作用是将原来的内存空间自动释放

   
p.reset(new
int);

   
*p.get()
= 3;

   
//独占内存,也可以用nullptr的方式进行初始化

   
boost::scoped_ptr<int>
pA(nullptr);

   
cout << *p.get()
<< endl;

 

   
cin.get();

   
//运行结果:

   
//12

   
//3

}

2. boost::scoped_array,通过它来智能管理数组

#include
<iostream>

//#include<boost/scoped_ptr.hpp>

#include
<boost/scoped_array.hpp>

//#include<boost/shared_ptr.hpp>

//#include<boost/shared_array.hpp>

//#include<boost/weak_ptr.hpp>

//#include<windows.h>

 

using
namespace
std;

 

void
main()

{

   
//某些情况下可以用作用域数组:管理数组比较方便,可以有效的释放数组

   
//同样会自动释放数组

   
boost::scoped_array<int>
p(new
int[10]);

   
//下面的方式是错的,因为这个指针是独享的,不可以共享给别的指针

   
//boost::scoped_array<int> pA(p);

   

   
*p.get()
= 1;

   
p[3] = 2;

   
//结果为2

   
cout <<
p[3] <<
endl;

   
//重新分配内存,这种指针独享指针,自动释放内存

   
p.reset(new
int[5]);

 

   
cin.get();

}

3.共享指针boost::shared_ptr

#include
<iostream>

#include
<vector>

//#include<boost/scoped_ptr.hpp>

//#include<boost/scoped_array.hpp>

#include
<boost/shared_ptr.hpp>

#include
<boost/shared_array.hpp>

#include
<algorithm>

//#include<boost/weak_ptr.hpp>

//#include<windows.h>

 

using
namespace
std;

 

//boost::shared_ptr<int>p共享一个指针

void
show(boost::shared_ptr<int>
p)

{

   
cout << *p
<< endl;

}

 

 

void
main()

{

   
vector<boost::shared_ptr<int>>
v;

   
boost::shared_ptr<int>
p1(new
int(11));

   
boost::shared_ptr<int>
p2(new
int(12));

   
boost::shared_ptr<int>
p3(p2);//拷贝

 

   
v.push_back(p1);

   
v.push_back(p2);

   
v.push_back(p3);

 

   
for_each(v.begin(),v.end(),show);

 

   
cin.get();

   
//运行结果:

   
//11

   
//12

   
//12

}

4. boost::shared_array,共享指针数组

#include
<iostream>

#include
<vector>

//#include<boost/scoped_ptr.hpp>

//#include<boost/scoped_array.hpp>

#include
<boost/shared_ptr.hpp>

#include
<boost/shared_array.hpp>

#include
<algorithm>

//#include<boost/weak_ptr.hpp>

//#include<windows.h>

 

using
namespace
std;

 

class
runclass

{

public:

   
int 
i = 0;

public:

   
runclass(int
num) :i(num)

   
{

       
cout <<
"i create" <<
i <<
endl;

   
}

   
runclass()

   
{

       
cout <<
"i create" <<
i <<
endl;

   
}

   
~runclass()

   
{

       
cout <<
"i delete" <<
i <<
endl;

   
}

   
void
print()

   
{

       
cout <<
"i =" <<
i <<
endl;

   
}

};

 

void
testfun()

{

   
boost::shared_ptr<runclass> 
p1(new
runclass(10));

   
boost::shared_ptr<runclass> 
p2(p1); 
//浅拷贝

   
boost::shared_ptr<runclass> 
p3(p1);

 

   
p1.reset(new
runclass(12));

   
p1->print();

   
p2->print();

   
p3->print();

}

 

void
testfunarray()

{

   
boost::shared_array<runclass>
p1(new
runclass[5]);

   
boost::shared_array<runclass>
p2(p1);

}

 

void
main()

{

   
testfun();

 

   
cout <<
"--------" <<
endl;

 

   
testfunarray();

 

   
cin.get();

}

运行指针:

5.弱指针(协助共享指针的管理)

#include
<iostream>

//#include<vector>

//#include<boost/scoped_ptr.hpp>

//#include<boost/scoped_array.hpp>

#include
<boost/shared_ptr.hpp>

//#include<boost/shared_array.hpp>

//#include<algorithm>

#include<boost/weak_ptr.hpp>

#include
<windows.h>

 

using
namespace
std;

 

//DWORD在:#include<windows.h>中,其本质是unsignedlong

DWORD 
WINAPI
reset(LPVOID
p)

{

   
boost::shared_ptr<int
> *sh =
static_cast<boost::shared_ptr<int
> *> (p);

   
sh->reset();//指针的重置,释放内存

   
std::cout
<< "指针执行释放"
<< endl;

   
return 0;

}

 

DWORD
WINAPI
print(LPVOID
p)

{

   
boost::weak_ptr<int
> * pw =
static_cast<boost::weak_ptr<int
> *>(p);

   
boost::shared_ptr<int
> sh =
pw->lock();//锁定不可以释放

   
Sleep(5000);

   
if (sh)

   
{

       
std::cout
<< *sh <<
endl;

   
}

   
else

   
{

       
std::cout
<< "指针已经被释放"
<< endl;

   
}

 

   
return 0;

}

 

void
main()

{

   
boost::shared_ptr<int>
sh(new
int(99));

   
boost::weak_ptr<int
> pw(sh);

   
HANDLE
threads[2];

   
threads[0] =
CreateThread(0, 0,
reset, &sh,
0, 0);//创建一个线程

   
threads[1] =
CreateThread(0, 0,
print, &pw,
0, 0);

   
Sleep(1000);

 

   
WaitForMultipleObjects(2,
threads,
TRUE,
INFINITE);//等待线程结束

   
cin.get();

}

运行结果:

指针执行释放

 

时间: 2024-08-22 14:46:31

6.Boost之smartpointer的相关文章

【C/C++学院】0904-boost智能指针/boost多线程锁定/哈希库/正则表达式

boost_array_bind_fun_ref Array.cpp #include<boost/array.hpp> #include <iostream> #include <string> using namespace std; using namespace boost; void mainA () { array <int, 5> barray = { 1, 2, 3, 4, 5 }; barray[0] = 10; barray.at(4)

SmartPointer

很久没有写博客了,很多工作中的总结都写在了自己的记事本里,比较杂,也没反映到博客上.刚去引擎组但却由于总公司的决策原因,引擎组当建立3个月就被撤销了,我也就没能继续做引擎了.部门被撤,当前也没任务,这些天就闲着看书.公司最近准备开IOS项目,我也经过很多思想上的挣扎,终于决定接受转向IOS等移动方向. 原因是虽然自己喜欢编程,喜欢做3d引擎,但是机会可遇不可求.能做3d引擎固然好,但是我当前还是个应届毕业生,也没有完整的做过任何商业项目,所以做个新项目也是不屈才的,更何况,新的领域,新的技术是I

C++ 日志库 boost::log 以及 glog 的对比

该文章转自阿里ata精选文章,作者为苏樽 日志能方便地诊断程序原因.统计程序运行数据,是大型软件系统必不可少的组件之一.本文将从设计上和功能上对比 C++ 语言常见的两款日志库: boost::log 和 google-glog .   设计 boost::log 的设计主要有日志器( Logger ).日志核心( Logging core ). Sink 前后端( frontend, backend )组成.日志文本以及日志环境由日志器( Logger )负责搜集,日志核心负责处理日志数据(例

boost库之tokenizer的使用

    在tokenizer出现之前,如果我们要对一个字符串进行分割,可能要自己封装一个函数.如果有n种不同的分割规则,那么你要封装n个不同的分割函数--太麻烦了!现在好了,Boost库的tokenizer封装了统一的语法来分割字符串,并且提供了三种常用的分割方式,足以满足我们的绝大多数编程需求.    tokenizer主要由分割器和迭代器两部分组成.分割器用于指定字符串的分割规则,如果不指定任何分割器,则tokenizer默认将字符串以空格和标点(键盘上除26个字母(包括大小写)和数字外的其

背景建模技术(八):bgslibrary_vs2010_mfc中boost的安装与配置

一.boost的下载与安装 在玩BGS Library时,有一个MFC的项目,在编译的过程中出现如下图的错误提示: 即: 1>e:\bgslibrary-master\vs2010mfc\src\stdafx.h(50): fatal error C1083: Cannot open include file: 'boost/lexical_cast.hpp': No such file or directory 根本原因在于没有安装和配置boost,下面对bgslibrary_vs2010_m

并发编程(二):分析Boost对 互斥量和条件变量的封装及实现生产者消费者问题

请阅读上篇文章<并发编程实战: POSIX 使用互斥量和条件变量实现生产者/消费者问题>.当然不阅读亦不影响本篇文章的阅读. Boost的互斥量,条件变量做了很好的封装,因此比"原生的"POSIX mutex,condition variables好用.然后我们会通过分析boost相关源码看一下boost linux是如何对pthread_mutex_t和pthread_cond_t进行的封装. 首先看一下condition_variable_any的具体实现,代码路径:/

在VS中使用Boost库出现Macro redefinition错误如何解决

最近使用Boost库做多线程开发,可视在vs中编译工程师总是遇到Macro redefinition错误,类似下面的错误描述 1>c:\program files (x86)\microsoft visual studio 10.0\vc\include\stdint.h(116): warning C4005: 'INT8_C' : macro redefinition 1>         c:\vc\include\boost\cstdint.hpp(376) : see previou

Eclipse &amp;amp; MinGW &amp;amp; Boost 配置

Boost作为C++的一个最关键的库, 是每一个C++编程人员需要使用的, 本文讲解如何在Eclipse+MinGW下配置boost库; 1. Eclipse安装CDT插件和MinGW 可以按照如下链接完美配置; 地址: http://blog.csdn.net/caroline_wendy/article/details/17039847; 2. 下载boost 下载boost最新版本至MinGW的文件下, 并解压, 最新版本: http://www.boost.org/users/histo

muduo 与 boost asio 吞吐量对比

muduo (http://code.google.com/p/muduo) 是一个基于 Reactor 模式的 C++ 网络库,我在编写它 的时候并没有以高并发高吞吐为主要目标,但出乎我的意料,ping pong 测试表明,muduo 吞吐量比 boost.asio 高 15% 以上. 测试对象 boost 1.40 中的 asio 1.4.3 asio 1.4.5 (http://think-async.com/Asio/Download) muduo 0.1.1 (http://muduo