How to implement the "extension of smart pointer" Two kinds of custom-made remove device

Source: Internet
Author: User

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&LT;T&GT;&AMP;&NBSP;SP): _ptr (Sp._ptr),  _pcount (sp._pCount) {+ + (*_pCount);} Sharedptr<t>& operator= (CONST&NBSP;SHAREDPTR&LT;T&GT;&AMP;&NBSP;SP)//traditional notation//{//if  (this &NBSP;!=&NBSP;&AMP;SP)//{//this->_releaSe ();//_pcount = sp._pcount;//_ptr = sp._ptr;//++ (*_pcount);//}//return *this;//} Sharedptr<t>& operator= (SHAREDPTR&LT;T&GT;&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;&LT;&LT;&NBSP;ENDL;} Void del () {cout <<  "Void del ()" &NBSP;&LT;&LT;&NBSP;ENDL;} Void close () {cout <<  "Void close ()" &NBSP;&LT;&LT;&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&LT;T&GT;&AMP;&NBSP;SP): _ptr (Sp._ptr),  _pcount (sp._pCount) {+ + (*_pCount);} Sharedptr<t>& operator= (SHAREDPTR&LT;T&GT;&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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.