Boost logo

Boost :

From: Robert Ramey (ramey_at_[hidden])
Date: 2004-05-07 10:49:13


Francois wrote:

> I am trying to use the serialization library to save an object and a
> pointer to the object, but the pointer has the type of the object's base
> class. Something like:

>// declaration
>MyClass myObject; // object, not pointer
>MyBaseClass *myPointer = &myObject;

>// serialization code
>ar & BOOST_SERIALIZATION_NVP(myObject);
>ar & BOOST_SERIALIZATION_NVP(myPointer);

>And I get an exception (either null reference exception or access
>violation) during the deserialization. I am using xml archives. The xml
>generated code seems fine.

I would expect and "unregistered_class" exception on both serialization and
de-serialization. If objects are to be serialized through a base class
pointer, they must either:

a) Be previously serialized directly or through the derived class pointer.
or
b) be "forward registered" with the archive.
Or
c) be "exported" via BOOST_CLASS_EXPORT

>template<class Archive>
>void serialize(Archive & ar, const unsigned int version)
> {
> ar & BOOST_SERIALIZATION_BASE_OBJECT_NVP(Base);
> // What is the intended purpose for the following?
> // I can't see why one would want to do this.
> /* uncomment to avoid the exception
> * ar & BOOST_SERIALIZATION_NVP(this);
> */
> }
>};

Robert Ramey


Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk