I have a class containing many different variables, for example there’s a few multidimensional vectors in there.
I’ve heard that you can store and load data directly to a file, but to what extent?
For example if I create an instance of this class, fill it up, then save it to a file, can I load it the same way? Like how does that work? Do I just save it all in one go or do I have to split up the data somehow?
This is not exactly beginner’s topic in C++
C++ has no automated way to store / load your objects to / from a file. Either way you chose to go, you’ll have to implement it yourself.
You might chose to overload the
<<an>>operators to use with streams, or you might want to go with your ownLoadandStoremethods (or whatever names you chose appropriate, like,Serialize/Deserialize). I personally prefer to create my own functions and not to use the operators, but it’s just me.Here is a simple example (with overloaded
<<and>>operators):It worth to mention that you should take care about the serialization format. In the example above it’s just text; but if you are going to store a lot of those objects, you might start thinking about serializing binary data (you’ll need to use
ofstream::binaryandifstream:binaryflags while opening the files, and will not need additional separators, like' 'andendlin your serialization stream).Typically, when you think of serialization, you also want to think of the versioning of your stream — and this is another separate topic.