1
class MyClass
{
   private:
      AnotherClass obj;
      float d;
      int k;

    public:
      MyClass(float d = 0.3, int k = 10);
      virtual ~MyClass();
      // other methods goes here
}

Is it possible to have a method that allow this class (MyClass) to save to its attribute values on the disk (hard drive) void storeOnDisk(string path){...} and another method which allow to load the attribute values from disk void loadFromDisk(string path) ?

If it is possible, should I also call loadFromDisk(path) in the constructor of MyClass (create another constructor maybe), and storeOnDisk(path) in the destructor of MyClass so that all current values can be saved when quitting the program that instanciated MyClass ?

5
  • It's called serialization. Commented Oct 29, 2013 at 14:58
  • Please have a look: Serialization Commented Oct 29, 2013 at 14:58
  • 1
    First question: yes. Second question, if you like, sure. But I think you would want to call storeOnDisk from the destructor to save the objects state before it is deleted, and loadFromDisk in the constructor, if the data happens to be available and you want to load in some previous state. Commented Oct 29, 2013 at 14:59
  • @BenJ yes loadFromDisk in constructor, my error. Commented Oct 29, 2013 at 15:02
  • wikipedia page Commented Oct 29, 2013 at 15:42

2 Answers 2

2

It depends on what exactly you want to achieve. However, usually, you'd not like to have such things in ctor/dtor since in C++ "copies" and "temporary objects" show up sometimes. Ctors/dtors are called when they are created/removed, just like regular objects, and it would also touch the files unless you prepare the code really well.

Often it is somewhat easier to keep a separate class that handles the reading/writing. Imagine a MyClassStorage class that would be a friend of the MyClass and that would contain only two methods: MyClass read(path) and write(path MyClass&).

If you like having it in single class or if you don't want to do everything manually, you can look at some serialization frameworks like Boost::Serialization. There are many short and easy examples on how to handle it, but - still - you'll have to read a bit about it first.

EDIT:

See http://www.boost.org/doc/libs/1_45_0/libs/serialization/doc/tutorial.html and the "A very simple case" section. It shows how to read/write a gps_position class. Note that this class iteself is very simple except that it contains an additional serialize function. This function works 'automagically' both as reader and writer. Since usually you want to read the same fields as you wanted to write, there's not need to say it twice (instead of saying read-A-B-C and write-A-B-C you say: handleThemForMe-A-B-C).

Then, in main you have example of usage. The text_oarchive and text_iarchive act as output and input files. Some gps_position object is created and named g, then saved to a file called filename, and then it is read back from the file as newg.

Actually, the ofstream line is somewhat too early and may be misleading. It is only for creating the oarchive and can be safely moved down like the ifstream/iarchive. It could look like that:

// create class instance
const gps_position g(35, 59, 24.567f);

/// ....

// save data to archive
{
    // create and open a character archive for output
    std::ofstream ofs("filename");
    boost::archive::text_oarchive oa(ofs);
    // write class instance to archive
    oa << g;
    // archive and stream closed when destructors are called
}

/// ....

// ... some time later restore the class instance to its orginal state
gps_position newg;
{
    // create and open an archive for input
    std::ifstream ifs("filename");
    boost::archive::text_iarchive ia(ifs);
    // read class state from archive
    ia >> newg;
    // archive and stream closed when destructors are called
}
Sign up to request clarification or add additional context in comments.

4 Comments

Is there any minimal simple working code that I can directly use for my example using Boost::Serialization ? Since I don't want to make something complicated.
@shn: I've updated the answer with a link to B:Ser tutorial from Docs and some explanation on how the basic example works.
Thanks. But what if my class contain a member which is vector< vector<float> > ?
See ie Serializing STL collections chapter here they precisely talk about that. You'll need to #include some extra headers. And, probably, that's all.
2

It is and you may use http://www.boost.org/doc/libs/1_34_0/libs/serialization/doc/index.html. (It is doing even more you expect)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.