Navin
Navin

Reputation: 534

Boost Deserialization into Dynamic types

In this link you can find the boost Serialization and Deserialization. But in the Deserialization method we need to give the specific object of the class to deserialize. Ex: newg

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
}

Since C++ doesnt have any base class like Object in C#, how can i deserialize into a common object and then typecast it?

As far as i no, i can create a custom class call object and inherit all the other classes from that, but even in that case there will be a situation to deserialize 2 times. Are there any workaround for this problem?

Upvotes: 2

Views: 1281

Answers (2)

eyck
eyck

Reputation: 36

You can deserialize into a base class if you serialize/deserialize pointers to objects. Boost then creates the Id etc. automatically. The mechanism is described here

Upvotes: 0

Evgeny Lazin
Evgeny Lazin

Reputation: 9413

Boost.serialization must be given concrete class. Since c++ don't have virtual constructors, deserializer must know what object to create. Common ancestor won't help either. Object's must be created before deserialization.

If you want serialize and deserialize class hierarchies, you must explicitly write class Id when serializing objects and when deserializing - explicitly read this Id to make a decision - what object to create and deserialize.

Upvotes: 3

Related Questions