How do I get around this without overloading the new operator on the class.
I have a basic C++ class that has storage and behaviour. As it is a factory class it needs to hit a database upon construction. Problem is that this can of course fail and the database library uses exceptions!
If I am not calling new everything is fine but if I use new to do dynamic allocation on the heap I run into the following scenario:
p = new MyClass(i);
1) Local storage is allocated on the heap.
2) Constructor is called with i as parameter.
3) Constructor fails with an exception which I catch.
All seems well except that the class local storage is never freed and the assignment to p is never made so I cannot free it either!
I know that I can get around this by overloading the new operator, creating a default constructor that only allocates space and then calling that and after that ...
It just seems to me that there must be a better way ?