This is my assignment question
Create a class with its own operator new. This operator should
allocate 5 objects, and on 5th ‘run out of memory’ and throw an
exception. Also add a static member function that reclaims this
memory. Now create a main () with try block and catch clause that
calls the memory-restoration routine. Put these inside a while loop to
demonstrate recovering from an exception and continuing execution.
Now I don’t want the program, but I am confused with this question. I can handle new operator overloading, but as asked I should create a static member “release()”. And if I have to recover in catch, how do I recover? I mean what object should I delete.
Or my prototype for release() is wrong?
Edit:
Also if I have to delete, which of the 5 objects can I delete? And deleting any object would in fact be incorrect. I see no way of recovery.
Sounds like you would need something like a static list of addresses in your class. Whenever new is called you store the address of the memory block in that list. Then in your release method you go through the static list and free the memory plus throw the exception.