将内容保存到文本文件中(最佳方法)

时间:2019-10-30 22:57:23

标签: c++

让我们说我有一个课程,我必须将其某些内容保存在文本文件中 稍后还原对象(使用适当的.load()方法):

class A {
//stuff and costructors

int key;  //Data to save

//Approach 1
int A.save(ostream& os) {
   os << this->key;
   return 0;

   }

//Approach 2
friend ostream& operator<<(ostream& os, const A& a);
};

ostream& operator<<(ostream& os, const A& a) {
   os << a.key;
   return os;
} //And then just ofile << mya;

它们有什么不同吗?如果是,什么是最好的?

0 个答案:

没有答案