A reference count is a counter that is associated with a ThisIsA so that it knows how many WhereIs are pointing at it. When a ThisIsA is not longer pointed to by any WhereIs, then it can be deallocated.
Listing 3 is the revised code to maintain and make use of the referenc count.
Listing 3: | WhereIs.03 |
First, nuke becomes private. This is because there should not be any need to explicitly deallocate a ThisIsA. Next, detach is enhaneced to first decrement the reference count of a ThisIsA. If the reference count decrements to 0, then the ThisIsA is deallocated by calling nuke.