C + + reference count

Source: Internet
Author: User

//reference count, Copy-on-write#include <stdio.h>#include<iostream>#include&lt, .... /require.h>#include<string>using namespacestd;classdog{stringnm; intRefCount; Dog (Const string&name): NM (name), RefCount (1) {cout<<"Creating Dog:"<< * This<<Endl; }    //Prevent assignment:dog&operator=(Constdog&RV); Public:    //Dogs can created on the heap    Staticdog* Make (Const string&name) {        return NewDog (name); } Dog (Constdog&d): NM (d.nm+"Copy"), RefCount (1) {cout<<"Dog Copy-constructor:"<< * This<<Endl; }    ~Dog () {cout<<"attached Dog:"<< * This<<Endl; }    voidAttach () {++RefCount; cout<<"Attach Dog:"<< * This<<Endl; }    voiddetach () {require (RefCount!=0); cout<<"detaching Dog:"<< * This<<Endl; //Destroy Object If no one is using it:        if(--refcount = =0)Delete  This; }    //conditionally copy this dog//Call before modifying the dog,assign//resulting pointer to your dog*dog*Unalias () {cout<<"unaliasing Dog:"<< * This<<Endl; //Not duplicate if not aliased;        if(RefCount = =1)            return  This; --RefCount; //Use copy-constructor to duplicate:        return NewDog (* This); }    voidRenameConst string&newName) {NM=NewName; cout<<"Dog renamed to:"<< * This<<Endl; } Friend Ostream&operator<< (ostream& OS,Constdog&d) {returnOS <<"["<< d.nm <<"],rc="<< D.refcount <<Endl; }};classdoghouse{Dog*p; stringHousename; Public: Doghouse (Dog* Dog,Const string&House ): P (dog), Housename (house) {cout<<"Created Doghouse:"<< * This<<Endl; } Doghouse (Constdoghouse&dh): P (DH.P), Housename ("copy-constructed"+dh.housename) {p-attach (); cout<<"Doghouse Copy-constructor:"<< * This<<Endl; } Doghouse&operator=(Constdoghouse&DH) {        //Check for self-assignment:        if(&dh! = This) {Housename= Dh.housename +"assigned"; //Clean up how you ' re using first:P->detach (); P= DH.P;//Like copy-constructorP->attach (); } cout<<"Doghouse operator=:"<< * This<<Endl; return* This; }    //Decrement RefCount, conditionally destroy~Doghouse () {cout<<"Doghouse destructor:"<< * This<<Endl; P-detach (); }    voidRenamehouse (Const string&newName) {Housename=NewName; }    voidUnalias () {p= p->Unalias (); }    //Copy-on-write.anytime you modify the//contents of the pointer you must first unalias it:    voidRenamedog (Const string&newName)        {Unalias (); P-rename (newName); }    //... or when you allow someone else access:dog*Getdog () {unalias (); returnp; } Friend Ostream&operator<< (ostream& OS,Constdoghouse&DH) {        returnOS <<"["<< Dh.housename <<"] contains"<< *dh.p; }};intMain () {Doghouse Fidos (Dog::make ("Fido"),"Fidohouse"), Spots (Dog::make ("Spot"),"Spothouse"); cout<<"entering Copy-construction"<<Endl;    Doghouse Bobs (Fidos); cout<<"After copy-constructing bobs"<<Endl; cout<<"Fidos:"<< Fidos <<Endl; cout<<"Spots:"<< spots <<Endl; cout<<"bobs:"<< Bobs <<Endl; cout<<"Entering spots = Fidos"<<Endl; Spots=Fidos; cout<<"After Spots=fidos"<<Endl; cout<<"Spots:"<< spots <<Endl; cout<<"entering Self-assignment"<<Endl; Bobs=bobs; cout<<"After self-assignment"<<Endl; cout<<"bobs:"<< Bobs <<Endl; //comment out the following linescout <<"entering rename (\ "Bob\")"<<Endl; Bobs.getdog ()->rename ("Bob"); cout<<"After rename (\ "Bob\")"<<Endl;}

C + + reference count

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.