Simple implementation of C + + smart pointers

Source: Internet
Author: User

    • 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&AMP;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-&GT;_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-&GT;_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&AMP;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-&GT;_CNT)--; if(* This->_cnt = =0) {            Delete  This-_cnt; Delete  This-_ptr; }    }     This->_cnt =ptr._cnt;  This->_ptr =ptr._ptr; (* This-&GT;_CNT) + +; return* This;} Template<classT>T& Smart_ptr<t>::operator*() {//pointer Manipulation    return*( This-_ptr);} Template<classT>T* SMART_PTR&LT;T&GT;::operator() {//pointer Manipulation    return  This-_ptr;} Template<classT>intSmart_ptr<t>:: get_cnt () {return* This-_cnt;}

Main function:

intMain () {smart_ptr<int&GT;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&GT;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

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.