0

I saw some programs including <iostream> and <ostream> simultaneously. Why?

Thanks for your kind reply.

Justin Ethier
  • 131,333
  • 52
  • 229
  • 284
Josh Morrison
  • 7,488
  • 25
  • 67
  • 86

3 Answers3

2

Why? Probably because it originally included just ostream and someone figured out it had to use input streams as well. Then they just didn't bother to remove the ostream include.

Or maybe they actually needed the concrete cin/cout/cerr stream objects which are defined in iostream separately to the stuff in istream/ostream, and they didn't realise that iostream pulls in both istream and ostream before defining those objects.

Without asking the author, it's hard to say, but those are at least two viable possibilities.

paxdiablo
  • 854,327
  • 234
  • 1,573
  • 1,953
2

Someone forgot to remove a header probably. You should always include only what you need in an implementation file but sometimes stuff gets left behind because people are lazy and/or don't know any better.

You should remove the one that is not needed.

Edward Strange
  • 40,307
  • 7
  • 73
  • 125
  • You don't _need_ to remove it since that will have zero effect. `iostream` itself includes `istream` and `ostream` and the standard mandates that implementations must take care of multiple inclusions. My advice would be to leave what isn't broken alone, if only for the sake of not needing an edit in source control. Next time you _have_ to edit the file, think about removing it. – paxdiablo Feb 22 '11 at 12:10
  • Bad advice. Extra, pointless code is confusing. – Edward Strange Feb 22 '11 at 17:16
2

<iostream> is not the combination of <istream> and <ostream>. It merely defines std::cin, std::cout and related objects. To actually do anything useful with std::cout, you still need <ostream>. Now, by C++ rules it's possible that some implementations actually do include <ostream> in <iostream>, but you shouldn't rely on this.

MSalters
  • 173,980
  • 10
  • 155
  • 350
  • This is an almost-duplicate of http://stackoverflow.com/questions/4930176/why-would-i-include-iostream-and-ostream-separately – MSalters Feb 23 '11 at 23:48