【问题标题】:Difference between fstream, ofstream, ostream, iostreamfstream、ofstream、ostream、iostream的区别
【发布时间】:2017-10-11 14:03:02
【问题描述】:

我正在参加一个关于 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 对象包含可以作为参数传递的数据(char 等)一样简单?

任何信息将不胜感激。

【问题讨论】:

标签: c++ fstream iostream ofstream ostream


【解决方案1】:

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-12-29
    • 1970-01-01
    相关资源
    最近更新 更多