//reference count, Copy-on-write#include <stdio.h>#include<iostream>#include<, .... /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