我一直在用 C++ 编写一些对文件进行操作的类。当我开始时,我让我的所有构造函数都接受一个std::string
被解释为文件名的参数,因为这既快速又简单。
std::fstream
任何类使用的唯一特定功能是.open()
. 我认为,因为std::ofstream
andstd::ostringstream
都派生自std::ostream
并且我只使用's 函数,所以让每个构造函数适当地使用or参数std::ostream
会更好,而不是让类绑定到文件。std::ostream
std::istream
一个很好的教科书示例,说明了多态性为何有用。不过,GCC 对此非常不满。这是我的代码的简化:
#include <iostream>
#include <fstream>
class Foo {
public:
Foo(std::istream&);
std::istream& input;
};
Foo::Foo(std::istream& is) {
this->input = is;
}
int main() {
std::ifstream is("test.txt");
Foo foo(is);
}
其中,当使用 编译时g++ test.cpp
,会产生以下丰富多彩的错误:
test.cpp: In constructor ‘Foo::Foo(std::istream&)’:
test.cpp:10:1: error: uninitialized reference member ‘Foo::input’ [-fpermissive]
In file included from /usr/include/c++/4.7/ios:43:0,
from /usr/include/c++/4.7/ostream:40,
from /usr/include/c++/4.7/iostream:40,
from test.cpp:1:
/usr/include/c++/4.7/bits/ios_base.h: In member function ‘std::basic_ios<char>& std::basic_ios<char>::operator=(const std::basic_ios<char>&)’:
/usr/include/c++/4.7/bits/ios_base.h:791:5: error: ‘std::ios_base& std::ios_base::operator=(const std::ios_base&)’ is private
In file included from /usr/include/c++/4.7/ios:45:0,
from /usr/include/c++/4.7/ostream:40,
from /usr/include/c++/4.7/iostream:40,
from test.cpp:1:
/usr/include/c++/4.7/bits/basic_ios.h:64:11: error: within this context
In file included from /usr/include/c++/4.7/iostream:41:0,
from test.cpp:1:
/usr/include/c++/4.7/istream: In member function ‘std::basic_istream<char>& std::basic_istream<char>::operator=(const std::basic_istream<char>&)’:
/usr/include/c++/4.7/istream:56:11: note: synthesized method ‘std::basic_ios<char>& std::basic_ios<char>::operator=(const std::basic_ios<char>&)’ first required here
test.cpp: In constructor ‘Foo::Foo(std::istream&)’:
test.cpp:11:16: note: synthesized method ‘std::basic_istream<char>& std::basic_istream<char>::operator=(const std::basic_istream<char>&)’ first required here
我无法筛选所有模板参数,但看起来它不喜欢this->input = is;
. 每个类能够作为成员访问流是很重要的,因为各种不同的成员函数都必须能够看到它。我认为 GCC 可能会更好地处理指针,但对我来说,指向类的指针看起来很像动态分配的内存,如果它们是真的,我只想提出这些怀疑。解决这个问题的正确方法是什么?