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();
}
運行結果:
指針執行釋放
?總結
以上是生活随笔為你收集整理的6.Boost之smartpointer的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 5.Boost之“资源申请即初始化”
- 下一篇: 8.Boost之unordered_se