建设部网站官网,设计排版网站,王建设医生网站,网站开发要注意的漏洞实现自己的智能指针
//智能指针 保证能做到资源的自动释放
//利用栈上的对象出作用域自动析构的特征#xff0c;来做到资源的自动释放的
templatetypename T
class CSmartPtr
{
public:CSmartPtr(T *ptr nullptr):mptr(ptr) {}~CSmartPtr() { delete mptr; }
privat…实现自己的智能指针
//智能指针 保证能做到资源的自动释放
//利用栈上的对象出作用域自动析构的特征来做到资源的自动释放的
templatetypename T
class CSmartPtr
{
public:CSmartPtr(T *ptr nullptr):mptr(ptr) {}~CSmartPtr() { delete mptr; }
private:T *mptr;
};int main()
{CSmartPtrint ptr1(new int);return 0;
}不能把智能指针new在堆上
CSmartPtrint *p new CSmartPtrint(new int); //p还是裸指针重载*和-
//智能指针 保证能做到资源的自动释放
//利用栈上的对象出作用域自动析构的特征来做到资源的自动释放的
templatetypename T
class CSmartPtr
{
public:CSmartPtr(T *ptr nullptr):mptr(ptr) {}~CSmartPtr() { delete mptr; }T operator*() { return *mptr; }T* operator-() { return mptr; }
private:T *mptr;
};int main()
{CSmartPtrint ptr1(new int);class Test{public:void test() { cout Test::test() endl; }};CSmartPtrTest test(new Test());test-test();return 0;
}/*不带引用计数的智能指针auto_ptr:C库里面C11新标准scoped_ptrunique_ptr*/auto_ptrint ptr1(new int);auto_ptrint ptr2(ptr1);*ptr2 20;cout *ptr1 endl; //ptr1现在为空//不推荐使用auto_ptr//scoped_ptr
//scoped_ptr(const scoped_ptrT) delete;
//scoped_ptrT operator(const scoped_ptrT) delete;unique_ptrunique_ptr(const unique_ptrT) delete;unique_ptrT operator(const unique_ptrT) delete;//实现了右值引用的拷贝unique_ptr(unique_ptrT src)unique_ptrT operator(unique_ptrT src)templatetypename Tunique_ptrT getSmartPtr(){unique_ptrT ptr(new T());return ptr;}unique_ptrint ptr1 getSmartPtrint();unique_ptrint p1(new int);unique_ptrint p2(std::move(p1));引用计数
//对资源进行引用计数的类
templatetypename T
class RefCnt
{
public:RefCnt(T *ptr nullptr):mptr(ptr){if (mptr ! nullptr)mcount 1;}//增加资源的引用计数void addRef(){mcount;}int delRef() { return --mcount; }
private:T *mptr;int mcount;
};//智能指针 保证能做到资源的自动释放
//利用栈上的对象出作用域自动析构的特征来做到资源的自动释放的
templatetypename T
class CSmartPtr
{
public:CSmartPtr(T *ptr nullptr):mptr(ptr) {mpRefCnt new RefCntT(mptr);}~CSmartPtr() { if (0 mpRefCnt-delRef()){delete mptr; mptr nullptr;}}T operator*() { return *mptr; }T* operator-() { return mptr; }CSmartPtr(const CSmartPtrT src):mptr(src.mptr), mpRefCnt(src.mpRefCnt){if (mptr ! nullptr)mpRefCnt-addRef();}CSmartPtrT operator(const CSmartPtrT src){if (this src)return *this;if (0 mpRefCnt-delRef()){delete mptr;}mptr src.mptr;mpRefCnt src.mpRefCnt;mpRefCnt-addRef();return *this;}
private:T *mptr; //指向资源的指针RefCntT *mpRefCnt; //指向该资源引用计数对象的指针
};
int main()
{CSmartPtrint ptr1(new int);CSmartPtrint ptr2(ptr1);CSmartPtrint ptr3;ptr3 ptr2;*ptr1 20;cout *ptr2 *ptr3 endl;return 0;
}循环引用
class B;
class A
{
public:A() { cout A() endl; }~A() { cout ~A() endl; }shared_ptrB _ptrb;
};class B
{
public:B() { cout B() endl; }~B() { cout ~B() endl; }shared_ptrA _ptra;
};int main()
{shared_ptrA pa(new A());shared_ptrB pb(new B());pa-_ptrb pb;pb-_ptra pa;cout pa.use_count() endl;cout pb.use_count() endl;return 0;
}定义对象的时候用强智能指针引用对象的地方使用弱智能指针
class B;
class A
{
public:A() { cout A() endl; }~A() { cout ~A() endl; }weak_ptrB _ptrb;
};class B
{
public:B() { cout B() endl; }~B() { cout ~B() endl; }weak_ptrA _ptra;
};int main()
{shared_ptrA pa(new A());shared_ptrB pb(new B());pa-_ptrb pb;pb-_ptra pa;cout pa.use_count() endl;cout pb.use_count() endl;return 0;
}weak_ptr只是观察作用观察资源还可不可用但是却不能用
class B;
class A
{
public:A() { cout A() endl; }~A() { cout ~A() endl; }void testA() { cout 非常好用的方法 endl; }weak_ptrB _ptrb;
};class B
{
public:B() { cout B() endl; }~B() { cout ~B() endl; }void func(){_ptra-testA();//使用不了}weak_ptrA _ptra;
};在使用时必须进行提升
class B
{
public:B() { cout B() endl; }~B() { cout ~B() endl; }void func(){//_ptra-testA();//使用不了shared_ptrA ps _ptra.lock(); //提升方法if (ps ! nullptr){ps-testA();}}weak_ptrA _ptra;
};//多线程访问共享对象的线程安全问题
class A
{
public:A() { cout A() endl; }~A() { cout ~A() endl; }void testA() { cout 非常好用的方法 endl; }
};void handler01(A *q)
{std::this_thread::sleep_for(std::chrono::seconds(2));//q访问A对象的时候需要侦测一下A对象是否存活q-testA();
}int main()
{A *p new A();thread t1(handler01, p);delete p;t1.join();return 0;
}class A
{
public:A() { cout A() endl; }~A() { cout ~A() endl; }void testA() { cout 非常好用的方法 endl; }
};void handler01(weak_ptrA pw)
{std::this_thread::sleep_for(std::chrono::seconds(2));shared_ptrA sp pw.lock();if (sp ! nullptr){sp-testA();}else{cout A对象已经析构不能访问 endl;}
}int main()
{{shared_ptrA p(new A());thread t1(handler01, weak_ptrA(p));t1.detach();}std::this_thread::sleep_for(std::chrono::seconds(20));return 0;
}/*
智能指针的删除器 deletor
智能指针能够保证资源绝对的释放
*/
//unique_ptr shared_ptr
/*
~unique_ptr() {是一个函数对象的调用 deletor(ptr); }
templatetypename T
class Deletor
{
public:void operator()(T *ptr){delete ptr;}
};
*/templatetypename T
class MyDeletor
{
public:void operator()(T *ptr) const{cout call MyDeletor.operator() endl;delete []ptr;}
};int main()
{unique_ptrint, MyDeletorint ptr1(new int[100]);return 0;
}templatetypename T
class MyFileDeletor
{
public:void operator()(T *ptr) const{cout call MyFileDeletor.operator() endl;fclose(ptr);}
};int main()
{unique_ptrFILE, MyFileDeletorFILE ptr(fopen(data.txt, w));return 0;
}使用lambda unique_ptrint, functionvoid(int*) ptr1(new int[100],[](int *p)-void {cout call lambda release new int[100] endl;delete[]p;});unique_ptrFILE, functionvoid(FILE*) ptr2(fopen(data.txt, w),[](FILE *p)-void {cout call lambda release fopen endl;fclose(p);});