I saw some programs including <iostream>
and <ostream>
simultaneously. Why?
Thanks for your kind reply.
<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.
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.
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.
精彩评论