|
Boost Users : |
From: Matthias Troyer (troyer_at_[hidden])
Date: 2007-05-16 09:41:06
T
On 16 May 2007, at 18:30, Ng Pan wrote:
> Hi Matthias
>
> It is so strange that all the derived objects are treated as base
> objects
> during serialization and deserialization. From the xml file, I
> found that
> all the class_id_reference are same within the vector.
That is expected since you serialize a base object by dereferencing a
pointer to the base class. The cast to the derived class is done only
when you serialize through the pointer, which is what you wanted to
avoid.
Now you will need to implement the dispatch to the serialization
function of the derived class yourself. You can do this either by
implementing a similar mechanism as used in Boost.Serialization - or
- if you use only one archive type - you could also write polymorphic
(virtual) save and load functions for your classes.
No matter what you do, you need to be extremely careful: there will
be serious problems if upon loading the type of derived class is
different than what it was when you had saved it. Suppose you have
two derived classes A and B. There will be problems if you serialized
an A and now try to deserialize a B through the base pointer. That is
why the serialization library has to construct the object pointed to.
You thus proceed at your own risk if you implement your idea.
Matthias
Boost-users list run by williamkempf at hotmail.com, kalb at libertysoft.com, bjorn.karlsson at readsoft.com, gregod at cs.rpi.edu, wekempf at cox.net