Boost logo

Boost Users :

Subject: Re: [Boost-users] [Serialization] How to serialize a derived type but deserialize it as a base type pointer?
From: Robert Ramey (ramey_at_[hidden])
Date: 2011-04-12 01:13:33

Jerry wrote:
> I know that we can serialize/deserialize a derived type through a
> base type pointer (using register_type()). But I cannot serialize a
> derived type and deserialize it through a base type pointer. It seems
> to me the data type in serialization and deserialization should be
> extactly the same one.

when you serialize through a derived type, extra information
about the derived type isn't saved as it's assumed it will
be deserialized the same way.

> What I want is,
> Base{...};
> Derived : Base{...};
> //Derived d;
Base *d = new Derived; // use this instead !!

> boost::archive::binary_oarchive oa(...);
> oa << d; // serialize a derived object
> Base *b;
> boost::archive::binary_iarchive ia(...);
> ia.register_type();
> ia >> b // Deseiralize to a base type pointer
> I have tried this but it does not work. Is there any way to make this
> work?
> Many thanks!!!

Boost-users list run by williamkempf at, kalb at, bjorn.karlsson at, gregod at, wekempf at