/* * SPtr.cpp * * Created on: Oct 4, 2012 * Author: mike */ #include "SPtr.hpp" //------------------------------------------------------------------------------ // Make *this point to the same T object as p. void SPtr::attach(SPtr& p) { count = p.count; target = p.target; // copy pointer (*count)++; } //------------------------------------------------------------------------------ // Make *this no longer point to anything, and delete the count and target // if this was the last pointer to that object. void SPtr::detach() { if (*count == 1) { cout << "releasing SPtr-controlled storage" << endl; delete count; delete target; } else { (*count)--; } // Okay to leave dangling pointer since detach() is private and is // only called from the destructor, copy constructor, and assignment // operator (which overwrite count and target immediately afterwards). // count = nullptr; // don't leave dangling pointers // target = nullptr; }