开发者

C++ headers - Why would a program include both iostream and ostream? [duplicate]

开发者 https://www.devze.com 2023-02-12 07:00 出处:网络
This question already has answers here: Why would I include iostream and ostream separately? [duplicate]
This question already has answers here: Why would I include iostream and ostream separately? [duplicate] (2 answers) 开发者_Python百科 Closed 6 years ago.

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.

0

精彩评论

暂无评论...
验证码 换一张
取 消