I'm an IT student working on a system design project. It's a hierarchy of classes all inheriting from a base-class called Object (so we can use polymorphism). As part of the specs, we have a NullObject class that needs to be implemented as a singleton.
The NullObject is used frequently in containers and is treated just like other Objects. This includes some deleting, which is where the problems start.
I overloaded the delete operator to only delete the instance when a reference count is zero. It works fine if you only call delete as an operator, as in
Object *obj = NullObject::getInstance(); //obj points to instance
but if you just call it as
it first calls the destructor. The memory isn't deallocated, but the NullObject loses its "objectness". If I have another pointer to the instance (which I have), it is no longer treated as a NullObject. (In my program, it gets treated as an Object, which is abstract - no good).
Most singleton tutorials simply say that singletons don't usually get deleted in the middle of programs and don't address the issue at all. Is there a way to stop the destructor from being called? Or should I change all the deletes to operator deletes (overloaded for Object)?