The so-called custom-made delete is to provide the target template with a free choice of the destructor interface, the advantage is that the smart pointer template can no longer only manage memory separately, we can also use it to manage the file pointers and other things. There are two ways to implement it, here we take the share pointer for example.
1. Pass the template parameter of a class and give the vacancy a value, the template sets the class as a member variable, which is instantiated by the class (delete Class), and the deletion method stored in the class is called.
The code is as follows:
#include <iostream>template <class T>struct Del{void operator () (const t*ptr) {if (PTR) {delete ptr;}}}; Struct free{void operator () (void* ptr) {cout << "Free:" << ptr << endl;free (PTR);}}; Struct close{void operator () (void *ptr) {cout << "Close" << endl ; fclose ((file*) (PTR));}; Template<class t, class deleter = del<t>>class sharedptr{public: Sharedptr (t* ptr): _ptr (PTR), _pcount (New long (1)) {}sharedptr (T* ptr, deleter del ): _ptr (PTR), _pcount (New long (1)), _del (del) {}~sharedptr () {_release ();} Sharedptr (CONST&NBSP;SHAREDPTR<T>&&NBSP;SP): _ptr (Sp._ptr), _pcount (sp._pCount) {+ + (*_pCount);} Sharedptr<t>& operator= (CONST&NBSP;SHAREDPTR<T>&&NBSP;SP)//traditional notation//{//if (this &NBSP;!=&NBSP;&SP)//{//this->_releaSe ();//_pcount = sp._pcount;//_ptr = sp._ptr;//++ (*_pcount);//}//return *this;//} Sharedptr<t>& operator= (SHAREDPTR<T>&NBSP;SP) {swap (_ptr, sp._ptr); Swap (_pCount, sp._pcount); return *this;} T& operator* () {return *_ptr;} T* operator-> () {return _ptr;} T* getptr () {return _ptr;} Long getcount () {return *_pcount;} Protected:void _release () {if (--(*_pcount) == 0) {//delete _ptr;_del (_ptr);d elete _pcount;}} protected:t* _ptr;long* _pcount;deleter _del;};
The test cases are as follows:
void Test2 () {sharedptr<int> SP1 (new int (1)); Sharedptr<int, Free> SP2 ((int*) malloc (sizeof (int) *), free ()); Sharedptr<file, close>sp3 = Std::fopen ("Test.txt", "w");}
2. In the template for the smart pointer, add a function pointer variable and use the constructor to determine the passed function pointer and delete it using the method pointed to by the pointer.
The code is as follows:
Void free () {cout << "Void free ()" &NBSP;<<&NBSP;ENDL;} Void del () {cout << "Void del ()" &NBSP;<<&NBSP;ENDL;} Void close () {cout << "Void close ()" &NBSP;<<&NBSP;ENDL;} Template<class t>class sharedptr{public:sharedptr (t* ptr): _ptr (PTR), _pCount (new long (1)) {}sharedptr (t* ptr, void (*p)): _ptr (PTR), _pcount (New long (1)), del (p) {}~sharedptr () {_release ();} Sharedptr (CONST&NBSP;SHAREDPTR<T>&&NBSP;SP): _ptr (Sp._ptr), _pcount (sp._pCount) {+ + (*_pCount);} Sharedptr<t>& operator= (SHAREDPTR<T>&NBSP;SP) {swap (_ptr, sp._ptr); Swap (_pCount, sp._pcount); return *this;} T& operator* () {return *_ptr;} T* operator-> () {return _ptr;} T* getptr () {return _ptr;} Long getcount () {return *_pcount;} Protected:void _release () {if (--(*_pcount) == 0) {del ();d elete _pCount;}} Protected:t* _ptr;long* _pcount;void (*del) ();};
The test cases are as follows:
void Test1 () {void (*p) (); int a = 0; SHAREDPTR<INT>P1 (&a, close);}
If there is any shortage or doubt, hope to discuss the message together, if there is no choice also hope to criticize correct.
This article is from the "Pawnsir It Road" blog, so be sure to keep this source http://10743407.blog.51cto.com/10733407/1757336
How to implement the "extension of smart pointer" Two kinds of custom-made remove device