Garbage collector Design

Source: Internet
Author: User

1automatic release with stack objects (you can disallow destructors to produce stack objects Oh, little tricks)Construct an empty smart pointer Class 2 by using the property of the stack object out-of-scope auto-release to solve the problem of intelligent release, as long as this does not have to manually delete the class pointer in the smart pointer class destructor inside the Delete object inside the object to release the garbage pointer, and so on. 3To solve the call problem of a function that is the real object of the smart pointer proxyThe overload-so operator, so that it can call directly the function in the object stored by this smart pointer, or use the proxy mode but there is no overloaded method simple  4 inherit the reference count class, to add reference to the technical function for itself, the reference count of + +-in the smart pointer to complete  # Include <iostream> #include <string.h>using namespace std;/* smart pointer garbage collector */ class Cmystring; class crefcount{    public:       //share real value New Add object         Crefcount (cmystring* re ALPTR): MCount (0), mpmystring (0) {            if (realptr!=null) {        & nbsp       mpmystring=realptr;           }       }  &NB Sp     Virtual ~crefcount () {                      &NBSP ; Delete mcount;       }        void Addrefencecount () {            if (mcount==null) {                Mcount=new int (0);    &NB Sp      }  &NBsp         + + (*mcount);       }         int Subrefencecount () {           /* 0 no objects share real value mbuf so you can release this space.   The virtual destructor for this is overridden by a derived class, so the derived class that first falls off */  &nb Sp       &NBSP;IF ((--(*mcount)) ==0) {                Delete this;                return 0;           }           return *mcount;       }    private:        int* MCo unt;        cmystring* mpmystring;};  class cmystring:public crefcount{public:    cmystring (char* str): Crefcount (this) {        char* p=new Char[strlen (str) +1];        mbuf=p;       :: strcpy (MBUF,STR);    }   /*       Virtual destructor easy to call, there is an inheritance relationship when convenient derived class to the base class pointer automatically call the baseclass, the virtual destructor will give     virtual table assignment first, so the current virtual table calls its own virtual function, and then call the virtual function of the base class     */    virtual ~cmystring ( ) {        if (mbuf!=null) {            Delete mbuf;      &N Bsp }   }    void fun1 () {        cout<< "Hello smart" <<endl;    }private:    char* MBuf;};  class csmartptr{public:    Explicit csmartptr (cmystring* ptr=null) {        mobject= ptr;        if (ptr!=null) {            Ptr->addrefencecount ();       }   }    csmartptr (csmartptr& obj) {        MOBJECT=OBJ.M object;        Mobject->addrefencecount ();   }    ~csmartptr () {        if (mobject!=null) {            Mobject->subrefencecount ();  & NBsP    }   }   /* Overload--Returns a pointer to the object being proxied, in the call is the real object pointer,        actually should be->-&gt ; so but the C + + compiler gives you the right one----two-step     */    cmystring* operator-> () {        Return mobject;   }   /* equals to generate a new object, by default join shared real value */    csmartptr& operator= (const csmartptr& obj) {       //Release the original count         if (mobject! = NULL)     &NB Sp       Mobject->subrefencecount ();                Mobject = obj.mobject;         if (mobject! = NULL)             Mobject->a Ddrefencecount ();        return *this;   }   /*       Proxy mode has s Martptr a B C 4 classes only       have smartptr can call fun1 this function, then the other three classes how to call fun1       is very simple, first call Smartptr Fun1 put a The results of the fun1 of or B or C are returned or passed to them &NBSP;&NBSp   */    void fun1 () {        MOBJECT-&GT;FUN1 ();   }private:    CMy String* Mobject;};   void fun1 () {    csmartptr PA1 (New cmystring ("HelloWorld"));    csmartptr PA2 (PA1);     Csmartptr PA3 (New CMyString ("China)");    csmartptr pa4;    pa4=pa1;    retur n;}  int Main () {    fun1 ();   //pa->fun1 ();   //pa->fun1 ();    return 0 ;}  Summary:The smart pointer principle is to take advantage of the automatic release of the Stack object, plus the overloaded operator

Garbage collector Design

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.