C++ 在需要ifstream的地方传入fsstream
上面的代码无法编译,似乎我无法用C++ 在需要ifstream的地方传入fsstream,c++,casting,C++,Casting,上面的代码无法编译,似乎我无法用fstream对象初始化ifstream&?如果我这样做会怎么样: void foo(ifstream &ifs) { //do something } int main() { fstream fs("a.txt", fstream::in); foo(fs); //error, can't compile } foo(静态_cast(fs)); 或 foo(动态演播(fs)); 您可能希望foo()接受istream。如注
fstream
对象初始化ifstream&
?如果我这样做会怎么样:
void foo(ifstream &ifs)
{
//do something
}
int main()
{
fstream fs("a.txt", fstream::in);
foo(fs); //error, can't compile
}
foo(静态_cast(fs));
或
foo(动态演播(fs));
您可能希望foo()接受istream。如注释所示,这是ifstream和fstream的基本类型
foo(dynamic_cast<ifstream>(fs));
在cplusplus.com上有一个关于这些类的很好的参考:
- “输入流”
- “输入文件流类”
- “输入/输出文件流类”
ifstream
和of stream
都是从fstream
派生出来的。是的,这有点奇怪,但我想他们决定用fstream而不是iofstream:)。
foo(dynamic_cast<ifstream>(fs));
void foo( istream & is )