I have a class that will allocate some memory, open a file, populate a structure, and do some other house keeping.
My current thought is to do something like:
foo f; //set 2 flags, one that the memory allocation succeeded, one that there is no open file, so no operations are valid except open()
if(!f.open("filename")) then do something.
open() would do all the dirty work, and every method in the class, would first check to see if a valid file is open and ready for i/o. This to avoid things like
foo f;
f.fetch(); //or whatever would throw an error
The class will be in a static library or DLL, and will be used by many different programs in the Windows environment; although, I might, in my copious free time, try to port to Linux.
Am I going at this correctly?
One last question, in the case of a failure to open, I want the calling program to decide what to do with the error. The failure to open might even be anticipated, with some recovery routine. On the other hand when a programmer does
foo f;
f.fetch(); there is no file open, this is programmer's error. What is the best way to handle this? It should always stop execution. I would rather not have to make every call part of an if statement as in if(!f.fetch())... or if(f.valid())f.fetch();