C ++ ostream and ofstream conversions

My code has an ostream object, which is accumulated by various modules and is ultimately displayed on the console. I would also like to write this ostream object to a file, but I need to rewrite all this code using the ofstream object instead, or is there a way to convert one to another (maybe via stringstream ?)

For example, many of my existing functions look like

 ostream& ClassObject::output(ostream& os) const { os << "Details"; return os; } 

Is it possible to call this function with an ofstream object as an argument and, instead, an ofstream object ofstream accumulate information?

+4
source share
1 answer

Yes, you can. That point in the concept of OO is called a subtype of polymorphism . Because ofstream comes from ostream , each ofstream instance is simultaneously an ostream instance (conceptually). Therefore, you can use it wherever an ostream instance is ostream .

+8
source

All Articles