Simple implementation and application of C + + RCSP smart pointer

Source: Internet
Author: User

Smart pointer Implementation Code Source blog: "http://blog.csdn.net/to_be_better/article/details/53570910"

Modify: Add a Get () function to get the raw pointer (raw pointer).

Other sources of thought "effective C + +"

The implementation code for smart pointers is as follows:

Template <typename t>classsmartptr;template<typename t>classptr{friendclassSmartptr<t>; T*m_ptr;    size_t M_count; PTR (T*p = NULL): M_ptr (P), M_count (1) {}    ~Ptr () {Deletem_ptr; }};template<typename t>classsmartptr{ Public: Smartptr (T*p = NULL): m_p (NewPtr<t>(P)) {} smartptr (ConstSmartptr &sp): m_p (sp.m_p) {++m_p->M_count; } smartptr&operator=(ConstSmartptr &sp) {        ++sp.m_p->M_count; if(--m_p->m_count = =0)        {            Deletem_p; } m_p=sp.m_p; return* This; } T*operator() {returnM_p->m_ptr;} ConstS Moperator()Const{returnM_p->m_ptr;} Toperator*() {return*m_p->m_ptr;} T*Get()/*Get raw pointer*/    {        returnM_p->m_ptr; }    ~smartptr () {if(--m_p->m_count = =0)            Deletem_p; }  Private: Ptr<T> *m_p;};

The reference count smart pointer (reference-counting smart pointer, RCSP) enables continuous tracking of how many objects are pointing to a resource and automatically deleting the resource when no one points to it.

In C + + resource management, resource leaks are caused by preventing accidental exits.

This reference counting can allow copying behavior, such as the need to suppress copying, the private way to inherit the Uncopyable class.

Uncopyable class:

class uncopyable{  protected:    uncopyable () {}    ~uncopyable () {}    Private:    uncopyable (const uncopyable &);     &operator= (const uncopyable &);};

An example of application:

The goal is to create a class of smart pointers to classes that describe some of the properties of a file, and use this pointer in subsequent code to give or read these properties. Of course, using smart pointers in order to prevent resource leaks is in line with the intent of this article.

By the member function: Createfileattrs () generates a static smart pointer dynamically, which is given by this pointer to the member variables in the class to allocate resources, and return this pointer, you can implement the function.

Test class:

classfileattr{ Public:    ~fileattr (); StaticSmartptr<fileattr>Createfileattrs (); Char*MD5;Private: FileAttr ();}; Fileattr::fileattr () {}fileattr::~fileattr () {cout<<"destructor"<<Endl; Delete[] MD5;} Smartptr<FileAttr>Fileattr::createfileattrs () {StaticSmartptr<fileattr> FileAttr (Newfileattr ()); FileAttr-&GT;MD5 =New Char[ -]; returnfileattr;}

Application Method:

intMain () {smartptr<FileAttr> FA = Fileattr::createfileattrs ();//using smart pointers    /*fileattr *fa = Fileattr::createfileattrs (). get ();//or use raw pointer*/{memcpy (FA-&GT;MD5,"md51",4); } {memcpy (FA-&GT;MD5 +4,"md52",4); } cout<< fa->md5<<Endl; return 0;}

Print output:

Md51md52
destructor

Because the custom class does not overload operator=, it is appropriate to use the smart pointer directly, and the Get () function is called if the original pointer is needed.

Simple implementation and application of C + + RCSP smart pointer

Related Article

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.