Reference count implementation

Source: Internet
Author: User

When I look at more than tive C ++, there will be reference counting. Now I have implemented it myself.

Base class for completion count

/*************************************** * ********** Author: zhou Xiang * E-mail: 604487178@qq.com * blog: Drawing refcounter_h # define refcounter_h # include <iostream> class refcounter {public: refcounter (); Virtual ~ Refcounter () = 0; void plusoneref (); void minusoneref (); bool isshared () const; private: int m_refcounter ;}; # endif // refcounter_h /********************************** * **************** Author: zhou Xiang * E-mail: 604487178@qq.com * blog: Counter "refcounter. H "refcounter: refcounter (): m_refcounter (1) {} refcounter ::~ Refcounter () {} void refcounter: plusoneref () {+ m_refcounter;} void refcounter: minusoneref () {-- m_refcounter;} bool refcounter: isshared () const {return m_refcounter> 1 ;}

A class that requires reference counting

/*************************************** * ********** Author: zhou Xiang * E-mail: 604487178@qq.com * blog: Export object_h # define object_h # include <cstring> # include "refcounter. H "Class Object: Public refcounter {friend object operator + (const object & _ V1, const object & _ V2); Public: Object (const char * _ value); Virtual ~ Object (); object (const object & _ V); const char * classname () const; private: char * PD ;}; # endif // object_h /********************************** * **************** Author: zhou Xiang * E-mail: 604487178@qq.com * blog: Images "object. H "# include <iostream> Object: Object (const char * _ value) {Pd = new char [strlen (_ value) + 1]; strcpy (PD, _ value) ;} Object ::~ Object () {If (! Isshared () {Delete [] PD ;}} object operator + (const object & _ V1, const object & _ V2) {return object (STD :: string (_ v1.pd) + _ v2.pd ). data ();} object: Object (const object & _ v) {Pd = new char [strlen (_ v. PD) + 1]; strcpy (PD, _ v. PD);} const char * object: classname () const {return PD ;}

An outsourcing template that implements reference judgment encapsulates the logic

/*************************************** * ********** Author: zhou Xiang * E-mail: 604487178@qq.com * blog: Drawing refcounterwapper_h # define counter # include <iostream> template <typename T> class refcounterwapper {public: refcounterwapper (T * P );~ Refcounterwapper (); refcounterwapper (const T & _ V); refcounterwapper (const refcounterwapper & _ V); counter & operator = (const refcounterwapper & _ V ); const T * operator-> () const; const T & operator * () const; T * operator-> (); T & operator * (); Private: T * PD ;}; template <typename T> refcounterwapper <t >:: refcounterwapper (T * P): PD (P) {}template <typename T> refcounterwapper <t>: :~ Refcounterwapper () {If (! Pd-> isshared () {Delete PD;} else {Pd-> minusoneref () ;}} template <typename T> refcounterwapper <t> :: refcounterwapper (const T & _ v) {Pd = new T (_ V);} template <typename T> refcounterwapper <t >:: refcounterwapper (const refcounterwapper & _ V) {Pd = _ v. PD; Pd-> plusoneref ();} template <typename T> refcounterwapper <t> & refcounterwapper <t>: Operator = (const refcounterwapper & _ V) {If (this = & _ v) return * This; P D-> minusoneref (); If (! Pd-> isshared () {Delete PD;} Pd = _ v. PD; Pd-> plusoneref (); return * This;} template <typename T> const T * refcounterwapper <t >:: operator-> () const {return PD ;} template <typename T> const T & refcounterwapper <t>: Operator * () const {return * PD;} template <typename T> T * refcounterwapper <t> :: operator-> () {return PD;} template <typename T> T & refcounterwapper <t>: Operator * () {return * PD;} # endif // refcounterwapper_h

Classes with counting functions for external use

/*************************************** * ********** Author: zhou Xiang * E-mail: 604487178@qq.com * blog: Login refcounterobject_h # define refcounterobject_h # include "refcounterwapper. H "# include" object. H "class refcounterobject {friend refcounterobject operator + (const refcounterobject & _ V1, const refcounterobject & _ V2); Public: refcou Nterobject (const char * _ v = "object"); Virtual ~ Refcounterobject (); const char * classname () const; private: refcounterobject (const object & _ V); refcounterwapper <Object> PD;}; refcounterobject :: refcounterobject (const char * _ V): Pd (new object (_ V) {} refcounterobject ::~ Refcounterobject () {} refcounterobject operator + (const refcounterobject & _ V1, const refcounterobject & _ V2) {return refcounterobject (* _ v1.pd) + (* _ v2.pd ));} refcounterobject: refcounterobject (const object & _ V): Pd (_ v) {} const char * refcounterobject: classname () const {return Pd-> classname ();} # endif // refcounterobject_h

Test code

#include <iostream>#include "refcounterobject.h"using namespace std;int main(){    RefCounterObject v("zhou love cc");    cout << v.className() << endl;    RefCounterObject v2 = v;    cout << v2.className() << endl;    RefCounterObject v3(v2);    cout << v3.className() << endl;    RefCounterObject v4;    cout << v4.className() << endl;    v4 = v3;    cout << v4.className() << endl;    RefCounterObject v5 = v4 + v2;    cout << v5.className() << endl;}

The five objects share two objects, and they save the string values, respectively Zhou love CC and Zhou love cczhou love CC.

Analyzed memory with valgrind, no memory leakage

Analyze how many times an object's destructor is called

Five objects have two object objects, which are called twice,

The other time is

Refcounterobject V4; cout <v4.classname () <Endl; V4 = V3; // parses the object cout starting with V4 <v4.classname () <Endl; refcounterobject V5 = V4 + V2; // The underlying layer of the + operation generates a local object cout <v5.classname () <Endl;

The shared object is the object pointed to by the PD pointer in refcounterwapper.

Relationships between these classes

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.