简体   繁体   English

显示输入到std :: ostream

[英]Display inputs to std::ostream

Disclaimer : I am a complete C++ beginner, and if there is a similar answer to this question, please direct me to it, as I may have missed it, not knowing much in the way of theory. 免责声明 :我是一名完全的C ++初学者,如果对此问题有相似的答案,请直接向我介绍,因为我可能错过了,对理论的了解也不多。

Suppose I have a method which accepts a reference to an ostream : 假设我有一个接受对ostream的引用的方法:

printAllObjects(std::ostream& os);

I am assuming it makes changes to the ostream , so that one can print the list of all of the objects to a file, say. 我假设它对ostream进行了更改,以便可以将所有对象的列表打印到文件中。 (I might be wrong here) (我可能在这里错了)

Is there any way of seeing what it writes to the ostream ? 有什么办法查看它写入ostream吗? (via cout preferably)? (最好通过cout )?

std::cout is an std::ostream , so just pass std::cout to the function and you'll see what it does: std::cout std::ostream ,因此只需将std::cout传递给函数,您将看到它的作用:

printAllObjects(std::cout);

This flexibility is the very purpose of accepting a reference to std::ostream ! 这种灵活性正是接受对std::ostream的引用的目的。

Other stream types 1 inheriting from the std::ostream base include: std::ostream基继承的其他流类型1包括:

  • std::ofstream (for file output) std::ofstream (用于文件输出)
  • std::ostringstream (for string output). std::ostringstream (用于字符串输出)。

1 That's not to say that std::cout is a type; 1这并不是说std::cout是一种类型; it's not. 不是。 It's a special, global instance of std::ostream . 这是std::ostream的特殊全局实例。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM