6.Boost之smartpointer
来源:互联网 发布:游戏鼠标 知乎 编辑:程序博客网 时间:2024/06/14 05:17
1自己实现一个智能指针的功能
#include<iostream>
#include<string>
#include<vector>
#include<algorithm>
#include<functional>
#include<stdlib.h>
usingnamespacestd;
template<classT>
classpmy
{
public:
pmy()
{}
pmy(T *t)
{
p =t;
}
~pmy()
{
if (p != nullptr)
{
deletep;
}
}
T operator *()
{
return *p;
}
private:
T *p = nullptr;
};
classTest
{
public:
Test()
{
cout <<"Test create" <<endl;
}
~Test()
{
cout <<"Testdelete"<<endl;
}
};
voidrun()
{
pmy<Test>p(newTest);//智能指针,智能释放
//*p;
}
voidmain()
{
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>
usingnamespacestd;
voidmain()
{
//scoped_ptr(作用于指针,特点是独享)就是一个智能指针
//p用(new int)来初始化
boost::scoped_ptr<int>p(newint);//自动释放内存
*p = 12;
//get()获取指针指向的内容
cout << *p.get() << endl;
//reset()的作用是将原来的内存空间自动释放
p.reset(newint);
*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>
usingnamespacestd;
voidmain()
{
//某些情况下可以用作用域数组:管理数组比较方便,可以有效的释放数组
//同样会自动释放数组
boost::scoped_array<int>p(newint[10]);
//下面的方式是错的,因为这个指针是独享的,不可以共享给别的指针
//boost::scoped_array<int> pA(p);
*p.get() = 1;
p[3] = 2;
//结果为2
cout <<p[3] <<endl;
//重新分配内存,这种指针独享指针,自动释放内存
p.reset(newint[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>
usingnamespacestd;
//boost::shared_ptr<int>p共享一个指针
voidshow(boost::shared_ptr<int>p)
{
cout << *p << endl;
}
voidmain()
{
vector<boost::shared_ptr<int>>v;
boost::shared_ptr<int>p1(newint(11));
boost::shared_ptr<int>p2(newint(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>
usingnamespacestd;
classrunclass
{
public:
int i = 0;
public:
runclass(intnum) :i(num)
{
cout <<"i create" <<i <<endl;
}
runclass()
{
cout <<"i create" <<i <<endl;
}
~runclass()
{
cout <<"i delete" <<i <<endl;
}
voidprint()
{
cout <<"i =" <<i <<endl;
}
};
voidtestfun()
{
boost::shared_ptr<runclass> p1(newrunclass(10));
boost::shared_ptr<runclass> p2(p1); //浅拷贝
boost::shared_ptr<runclass> p3(p1);
p1.reset(newrunclass(12));
p1->print();
p2->print();
p3->print();
}
voidtestfunarray()
{
boost::shared_array<runclass>p1(newrunclass[5]);
boost::shared_array<runclass>p2(p1);
}
voidmain()
{
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>
usingnamespacestd;
//DWORD在:#include<windows.h>中,其本质是unsignedlong
DWORD WINAPIreset(LPVOIDp)
{
boost::shared_ptr<int > *sh =static_cast<boost::shared_ptr<int > *> (p);
sh->reset();//指针的重置,释放内存
std::cout << "指针执行释放" << endl;
return 0;
}
DWORDWINAPIprint(LPVOIDp)
{
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;
}
voidmain()
{
boost::shared_ptr<int>sh(newint(99));
boost::weak_ptr<int > pw(sh);
HANDLEthreads[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();
}
运行结果:
指针执行释放
- 6.Boost之smartpointer
- Boost学习笔记----SmartPointer之Boost::scoped_ptr
- smartPointer
- SmartPointer
- SmartPointer
- android smartpointer
- Boost学习之Boost简介
- 【boost学习】之boost::bind
- boost学习之Boost.Lambda
- Boost使用指南之Boost.Any
- boost之Boost.Foreach遍历
- boost之Boost.Foreach遍历
- 深度探索智能指针(SmartPointer)
- 清除vector中的非smartpointer
- 浅尝boost之format
- boost之option_description
- boost 之 scoped_ptr
- 学习boost之lexical_cast
- JAVA学习第四课(数组)
- 项目日志(5)关于进度总结及展望
- 安卓Activity的自定义标题的问题
- 4.Boost之ref
- 5.Boost之“资源申请即初始化” RAII
- 6.Boost之smartpointer
- uva 1585 - Score
- APUE 第三章
- leetCode: same tree
- HDOJ题目4985Little Pony and Permutation(置换群)
- 7.Boost之thread
- 8.Boost之unordered_set
- POJ 2400 KM最小权匹配+输出所有配对方案
- 9.Boost之正则regex