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:
Code:
// errfile is declared as ostream* errfile
if (file_given) {
    errfile = new ofstream(filename);
}
else {
    errfile = &cerr;
}
Is this a valid way of handling the error output?

Also in the class destructor, I do this:
Code:
if (errfile != &cerr) {
    delete static_cast<ofstream*>(errfile);
}
Any advice would be appreciated?

Thanks,
- Sean