We want to make it possible for C++ programs to have objects perform reference counting so that the objects delete themselves when they become unreachable by any pointer. No approach to this problem is perfect, but we will attempt a strategy that, by using templates, should work for any object without modification to its class.
To perform reference counting, we need to impose two important changes:
To achieve these ends, we must create two new classes:
Your assignment is to create these two classes. You will begin by grabing copies of the following files:
~sfkaplan/public/cs12/Tester.cc
~sfkaplan/public/cs12/Point.hh
~sfkaplan/public/cs12/Point.cc
Within Tester.cc is code that creates Point objects. It does so using RCObject and RCPointer so that Point objects that are no longer reachable delete themselves.
Your assignment: Create the RCObject and RCPointer classes. They must be written so that the code in Tester.cc compiles and runs. I strongly recommend that your new classes emit debugging information that indicates where the RCPointer objects point, and how the reference counts of RCObject objects change. These debugging statements should make it clear when an object deletes itself due to a zero reference count.
First, it is important to remember that template classes must be wholly contained in header files. Thus, you should be placing your code in two files: RCObject.hh and RCPointer.hh.
Second, you will find that RCPointer relies on RCObject. That is, consider the following line from Tester.cc:
RCPointer<Point> p1 = new RCObject<Point>;
Although the RCPointer is really going to point at an RCObject<Point>, we only pass the Point class as a template argument to RCPointer. It is simply assumed by RCPointer that the Point object will really be an RCObject<Point> object.
Consequently, Tester.cc includes only RCPointer.hh. In doing so, both header files are actually included.
Please note that this is not the normal or best solution to the problem of multiple inclusion. It works for this case, and should be sufficient for this project. If you are interested, however, to learn about the better solutions, just ask me.
Use the cs12-submit program to submit only your source code, which should have been written as RCObject.hh and RCPointer.hh files. Submit your work as project-4.