   reading and saving class with binary files
  Hi,I've recently read a tutorial which was showing that a class can be saved into binary file simply by using ofstream write function, where you pass a class instance as data and sizeof(this_class) as size. reading data from file was described in similar way.Althought i don't doubt it will work, it seems to me as a really bad way to do this.What will happen if i reorder variables within class, will the saved files no longer work?How about variables renaming? I'm not even mentioning a situation where you have to add variables to class. With my current code where i save and load everything manually, i can write a simple translator program which will load the old files and replace them by adding needed info. This doesn't seem to be possible with "dumping" whole class into binary file..So i wanted to ask you:Is it sometimes useful and used? Maybe in making game state saving/loading system?Does people actually use this method?And generally what's your opinion on this?   
  Bad idea. Consider classes with virtual functions, the compiler usually has to insert bookkeeping data into the class to keep track of this. This data is often implemented as a pointer, meaning that once written to a file it has no value and will cause undefined behaviour if such a class is read from the file. This is of course more obvious when the classes have pointers or references as members.For POD (plain old data) the method will work fine. As you mentioned if the classes members change you lose all data you had before.I haven't used it (my game has no save option, tough luck users =P), but boost's serialization seems to tick many of the things you may need.   ;Quote:Original post by ShugerAlthought i don't doubt it will work, it seems to me as a really bad way to do this.It is. It will only work with POD types, and recompiling your code with a different compiler (or even different compiler *options*) may cause it to stop reading it properly.Even if you don't need to do the things you mention in the rest of your post, it's better to write the object member by member, and realize that some members are going to need special care.Quote:What will happen if i reorder variables within class, will the saved files no longer work?Indeed. You're going to be reading, effectively, garbage.Quote:How about variables renaming? This doesn't matter; in C++, variable names don't exist in the compiled code (except possibly for debugging).Quote:And generally what's your opinion on this?My opinion is that you really need to read this. And then consider using this. :)   