Turning the question around a bit, what's common between
std::cout
and an arbitrary
std::ofstream
?
If you have a look at the documentation for both you'll see that they're instances of classes derived from
std::ostream
. In your examples all the operations you do are members of
std::ostream
so yep, there is a parameter type you can use which will do what you want - a reference to a
std::ostream
. So you could write:
void display_some_guff( std::ostream &output_to )
{
output_to << "I'm some output" << std::endl;
}
and then feed the function either
std::cout
or a
std::ofstream
.
So now you know how (okay, you know one way) to abstract out the thing you want to do the output to. However are you sure you're concentrating on abstracting out the right thing? If what you were interested in displaying was neatly parceled up in an object your code becomes:
std::cout << prog_state << std::endl;
file_stream << prog_state << std::endl;
and the whole question goes away or becomes a lot less pressing as you've solved it as a direct consequence of your object model.
Cheers,
Ash