由买买提看人间百态

boards

本页内容为未名空间相应帖子的节选和存档,一周内的贴子最多显示50字,超过一周显示500字 访问原贴
JobHunting版 - C++ 一问?
相关主题
One C++ questionGoogle Japan电面
请问关于overloading << (转载)求助,刚拿到offer,如何negotiate工资 (转载)
发发面经 攒人品 C++的amazon第三轮电面
问个c++题A tricky question
问一道kth smallest element的题目【c++里override输出<<总出错】
请教ebay 的面试题一道新鲜G面筋(2)
问个anagram的题目啊一个新鲜twitter面经
C++ Q22: ostream问一道题(2)
相关话题的讨论汇总
话题: print话题: std话题: void话题: ostream话题: output
进入JobHunting版参与讨论
1 (共1页)
h*****g
发帖数: 312
1
You have to implement a function that needs to be able to write to both
standard output (typically the console screen) and files. Which one of the
following function declarations satisfies that need?
A.
void print(std::ostream &os);
B.
void print(std::ofstream is);
C.
void print(std::cout);
D.
void print(std::istream is);
E.
void print(std::istream &is);
问下,B 为啥不行呢?A 可以输出到文件吗?网上没找到答案~~~
s*****n
发帖数: 231
2
cout is an object of class ostream that represents the standard output
stream.
and ostream is the base class of ofstream, which you use to output to a file.
So in A. void print(std::ostream &os);
if you pass a reference to ostream object in the print function, it's OK for
both type.
But in B.void print(std::ofstream is);
Not only you cannot output to the standard output, but also it's passed by
value, that means you make a copy of the ostream object you passed in, and
do output in that copy, which may not work as expected.
Please correct me if I am wrong, thanks.
h*****g
发帖数: 312
3
thank you so much! I've learned a lot!

file.
for

【在 s*****n 的大作中提到】
: cout is an object of class ostream that represents the standard output
: stream.
: and ostream is the base class of ofstream, which you use to output to a file.
: So in A. void print(std::ostream &os);
: if you pass a reference to ostream object in the print function, it's OK for
: both type.
: But in B.void print(std::ofstream is);
: Not only you cannot output to the standard output, but also it's passed by
: value, that means you make a copy of the ostream object you passed in, and
: do output in that copy, which may not work as expected.

1 (共1页)
进入JobHunting版参与讨论
相关主题
问一道题(2)问一道kth smallest element的题目
fb面经里的这个题有优于O(n^2)的解法么?请教ebay 的面试题一道
Can anyone help me analyze my case问个anagram的题目啊
programming pearl看不懂这个题C++ Q22: ostream
One C++ questionGoogle Japan电面
请问关于overloading << (转载)求助,刚拿到offer,如何negotiate工资 (转载)
发发面经 攒人品 C++的amazon第三轮电面
问个c++题A tricky question
相关话题的讨论汇总
话题: print话题: std话题: void话题: ostream话题: output