cout and ofstream

Just to be clear, from what I've tested, if I'm right, ofstream inherits from ostream, and cout is an ostream? And polymorphic behaviour occurs here with the rdbuf(/*Whatever type's returned by the rdbuf() method*/) method, so ofstream's version is private or protected and can't be accessed directly (but it can be accessed via ostream::rdbuf, of course)?
Last edited on
ofstream inherits from ostream

yes

and cout is an ostream

Yes, with clarification that "is a" is an OOP relationship: the runtime type of std::cout is some implementation-defined type, derived from std::ostream.

And polymorphic behaviour occurs here with the rdbuf

Kind of:
std::ostream implements formatted I/O interface (operator>>, put, write) in terms of the public interface of std::streambuf (which calls virtual member functions of std::streambuf, which is where polymorphic behavior occurs)

std::ofstream implements file-specific interface (open, close) in terms of the public interface of std::filebuf.
Topic archived. No new replies allowed.