我想重载 operator<< 以将对象序列化到文件(追加)。我应该使用哪个流? ofstream
还是 fstream
?有什么区别?
std::ofstream& operator<<(std::ofstream& ofs, const MyData&);
std::fstream& operator<<(std::fstream& fs, const MyData&)
谢谢 jack
最佳答案
您应该重载 ostream 的运算符,然后您可以自然地将它用于派生自该运算符的任何类的实例 - ofstream、fstream(继承自 iostream,它继承自 istream 和 ostream)、ostringstream 和 stringstream(继承iostream也是)
std::ostream& operator<<(std::ostream& os, const MyData&);
关于c++ - fstream 和 ofstream 运算符 << 区别,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3171588/