Search code examples
c++iovirtualiostreamoperator-keyword

C++: Are iostream operator<< and operator>>s for primitive types virtual?


As ifstream and istringstream are inherited from istream, I expected that istream (or rather, basic_istream) would declare operator>> for primitive types like this:

virtual istream& istream::operator>>(char &c); virtual istream& istream::operator>>(int &i);

A quick glance at the source suggests this is not the case -- these functions don't look to be virtual. Am I missing something?

[The context is that I am trying to roll istreams which perform binary I/O, correct for endianness, etc..]


Solution

  • No, they are not virtual. There are some virtual functions inside the basic stream functionality, but that's inside the parts that input/output the actual data, not the formatting of types to strings, which is what actually happens in operator>>.

    Edit: I believe all the virtual functionality is "hidden" from the public interfaces. basic_streambuf::pubseekpos is, at least in the gcc supplied headers as a call to a the virtual seekpos.