是否可以轻松地在C ++文件中保存类的实例?

时间:2014-12-17 00:20:15

标签: c++ file class save instance

假设我有一个类(包含另一个类的实例)

这是environment.h

class environment
{

    public:
    int historycounter = 0;
    vector<single_event> history; //containing a vector of type single_event (another class)
    bool insertion_started = false;

   private:

};

现在让我们假设我在主文件Main.cpp中创建了该类的实例 在Main:

#include "environment.h"
environment env1; //create an instance of the class environment
env1.history_counter = 5; //inelegant example to show that I'm inserting values to env1

我的问题:是否有一种将env1保存到文件中的简单方法? 在我幼稚的心中,我希望看到类似的东西:

SaveToFile(env1, "filename.whatever");
environment test;
LoadfromFile(test, "filename.whatever");

cout<<test.history_counter; //the goal is to have the number 5 printed

我希望有这样的方式

0 个答案:

没有答案