- The use of smart pointers: in C + +, using normal pointers is prone to the problem of heap memory leaks, that is, the programmer will forget to release, and two releases, the program is abnormal when the memory leaks and other issues, and use smart pointers to better manage heap memory. Note that here the smart pointer is a class rather than a real pointer, just wrapping a real pointer, acting on the original pointer. By overloading the operator, you can have a similar operation for the smart pointer and the real pointer.
- How to implement: in the smart pointer destructor, the original pointer of the agent can be deleted and assign null pointer, and so on, when the smart pointer is destroyed, the destructor is automatically called, eliminating the programmer's own management pointer operation. When multiple smart pointers proxy the same pointer, we will introduce a counter to count the number of smart pointers, and the last smart pointer to destroy the original pointer is obliged to destroy the work, the implementation code is as follows:
template<classT>classSmart_ptr { Public: Smart_ptr (T*ptr); Smart_ptr (ConstSmart_ptr &ptr);//copy Constructor~smart_ptr ();// Destructors intGet_cnt ();//gets the number of smart pointers for the current proxy's original pointersmart_ptr&operator=(ConstSMART_PTR&PTR);//Assignment Operationt&operator*();//pointer Manipulationt*operator();//pointer ManipulationPrivate: T*_ptr; int*_cnt;}; Template<classT>smart_ptr<t>::smart_ptr (t*ptr): _ptr (PTR) {//determines whether the parameter pointer is empty; not NULL, Count +1 if(_ptr) _cnt =New int(1); Else_cnt =New int(0);} Template<classT>smart_ptr<t>::smart_ptr (ConstSmart_ptr &ptr) {//copy constructor, copy succeeded, Count plus 1 if( This! = &ptr) { This->_ptr =ptr._ptr; This->_cnt =ptr._cnt; (* This->_CNT) + +; }}template<classT>smart_ptr<t>::~smart_ptr () {//if the current smart pointer is the last proxy pointer, it is responsible for destroying the original pointer(* This->_CNT)--; if((* This->_cnt) = =0) { Delete This-_cnt; Delete This-_ptr; _cnt=nullptr; _ptr=nullptr; }}template<classT>smart_ptr<T>& Smart_ptr<t>::operator=(ConstSMART_PTR&PTR) {//1: If the proxy pointer before and after the assignment is the same, directly return to 2: first determine whether the current smart pointer is the last agent of the original pointer smart pointer, if, destroy the original pointer; if( This->_ptr = = ptr._ptr)return* This;//3: The current smart pointer will proxy a new pointer if( This->_ptr! =nullptr) { (* This->_CNT)--; if(* This->_cnt = =0) { Delete This-_cnt; Delete This-_ptr; } } This->_cnt =ptr._cnt; This->_ptr =ptr._ptr; (* This->_CNT) + +; return* This;} Template<classT>T& Smart_ptr<t>::operator*() {//pointer Manipulation return*( This-_ptr);} Template<classT>T* SMART_PTR<T>::operator() {//pointer Manipulation return This-_ptr;} Template<classT>intSmart_ptr<t>:: get_cnt () {return* This-_cnt;}
Main function:
intMain () {smart_ptr<int>SP1 (New int(1)); cout<<"sp1.cnt:"<< sp1.get_cnt () <<Endl; Smart_ptr<int>SP2 (SP1); cout<<"sp1.cnt:"<< sp1.get_cnt () <<Endl; Smart_ptr<int>SP3 (New int(Ten)); cout<<"sp3.cnt:"<< sp3.get_cnt () <<Endl;; SP3=SP2; cout<<"sp3.cnt:"<< sp3.get_cnt () <<Endl; return 0;}
Call Result:
Simple implementation of C + + smart pointers