fstream,ofstream,ostream,iostream之间的区别

Difference between fstream, ofstream, ostream, iostream

本文关键字:区别 之间 ostream ofstream fstream iostream      更新时间:2023-10-16

我正在C++的入门级课程上进行测验,我正在尝试理解一个问题。 在互联网上搜索并没有得到答案后,所以我来了。

Which of the following function declarations will accept either cout or a file stream 
object as its argument?
A. void output( fstream &outFile);  
B. void output( ofstream &outFile); 
C. void output( ostream &outFile);  
D. void output( iostream &outFile);

答案是C。

我知道以下两者之间的区别:fstream,ofstream,ostream,iostream。

我不明白的是为什么其他选项都无法接受 cout 或文件流对象作为参数。

答案是否像 ostream 对象包含可以作为参数传递的数据(字符等)一样简单?

任何信息将不胜感激。

答案是C。问题是关于继承层次结构。 std::coutstd::ostream的一个实例。所有其他函数都接受 std::ostream 的子类,因此无法处理 std::coutstd::fstream可以传递给他们所有人,但问题是关于两者的。