Save objects from vector with pointers to file

I have a vector with pointers to a base class object so i can manage objects derived from that class.

vector <Product*>  products;

i am trying to write these objects to a file while iterating through the vector but i am not sure if this works correctly.

void Inventory ::  saveProductsToFile()
 {
    ofstream outfile;
    outfile.open("inventory.dat",ios::binary);

    list <Product*> :: iterator it;
    for(it=products.begin(); it!=products.end(); it++)
        outfile.write((char*)*(it),sizeof(Product));
 }

The file is created but i have no idea if i'm saving the actual objects themselves or their addresses.Is this correct or is there another way?

This is how the file looks like:

ˆFG "   H*c     \Âõ(œ@@pFG    h*c b'v      b@

Answers


You code can work. You cannot serialize polymorphic objects in that way. For starters, you're writing the hidden vptr out to disk; when you reread the data, it will not be valid. And you're only writing out the data in the base class (Product), because that's what sizeof(Product) evaluates to. And finally, just writing a byte image of anything but a char[] will probably mean that you won't be able to reread the data some time in the future (after a compiler upgrade, or a machine upgrade, or whatever).

What you have to do is to define a format (binary or text) for the file, and write that. For the basic types, you can start with something existing, like XDR or Protocol buffers, but neither of these work that well with polymorphic types. For polymorphic types, you have to start by defining how you identify the type in question when rereading. This can be tricky: there's nothing in std::type_info which helps, so you need some means of establishing a relationship between your (derived) types and the identifier. Then every derived class must implement a write function, which first writes its type, then writes its data out, one element by one. When reading, you read the type, look up the appropriate read function for that type in a map, and call that function, which then reads the data one by one.

Finally, I might point out that all successful serialization schemes I've seen depend on generated code. You describe your types in a separate file, or in special markup (in a specially marked comment in the C++), and have a program which reads that, and generates the necessary code (and often the actual classes you use).


Thats not how you "serialize" data. Like this the pointers are only valid during runtime or until you delete them (depending on what happens/stops first). Like this you wouldn't be able to restore your data, because after the program has stopped everything from your former memory becomes invalid. You would have to store the actual values from your class.


Need Your Help

c++ iostream - output file is not created

c++ iostream

I added some commands to produce an output file in methods of a given class. This worked perfectly well, and output file was produced during execution. Now that I made major changes to the code (bu...

Creating an expandable RecyclerView

android listview android-recyclerview expandablelistview

I'm trying to implement a recyclerview that behaves like my sketch below: