DogHouse& operator=(const DogHouse& dh) { // Check for self-assignment: if(&dh != this) { houseName = dh.houseName + " assigned"; // Clean up what you're using first: p->detach(); p = dh.p; // Like copy-constructor p->attach(); } cout << "DogHouse operator= : " << *this << endl; return *this; }
// Decrement refcount, conditionally destroy ~DogHouse() { cout << "DogHouse destructor: " << *this << endl; p->detach(); }