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();
}
运行结果:
指针执行释放