I have been designing a class and trying to come up with a better way of handling error output. If a file is specified, I want all errors to be written to the file, if no file is specified, I want the errors to be written to cerr. I have been using something like this:
Is this a valid way of handling the error output?Code:// errfile is declared as ostream* errfile if (file_given) { errfile = new ofstream(filename); } else { errfile = &cerr; }
Also in the class destructor, I do this:
Any advice would be appreciated?Code:if (errfile != &cerr) { delete static_cast<ofstream*>(errfile); }
Thanks,
- Sean