Boost logo

Boost Users :

From: Christian Rössel (christian.roessel_at_[hidden])
Date: 2006-10-24 05:09:48


Christian Henning schrieb:
> Hi Robert, thanks for the quick answer. I went for the second idea and
> made my base class abstract using a pure virtual member function.
> Unfortunately, now I'm catching an unknown exception when trying to
> deserialize.
>
> It seems to me that the serializing lib still doesn't know that it's
> deserializing an object of type derived2.
>
> Here is what I'm doing right know:
>
> #include <boost/archive/binary_iarchive.hpp>
> #include <boost/archive/binary_oarchive.hpp>
> #include <boost/archive/text_iarchive.hpp>
> #include <boost/archive/text_oarchive.hpp>
> #include <boost/serialization/serialization.hpp>
>
>
> class base
> {
> public:
> virtual void foo() =0;
> unsigned int type() { return _type; }
>
> protected:
>
> base() : _type( 0 ) {}
> base( unsigned int type) : _type( type ) {}
>
> private:
>
> friend boost::serialization::access;
>
> template< class ARCHIVE >
> void serialize( ARCHIVE& ar, const unsigned int version )
> {
> ar & _type;
> }
>
> private:
>
> unsigned int _type;
> };
>
> BOOST_IS_ABSTRACT( base )
>
> class derived1 : public base
> {
> public:
> derived1() : base( 1 ), _value( 2.1 ) {}
>
> virtual void foo() {}
>
> private:
>
> friend boost::serialization::access;
>
> template< class ARCHIVE >
> void serialize( ARCHIVE& ar, const unsigned int version )
> {
> ar & boost::serialization::base_object<base>( *this );
> ar & _value;
> }
>
> private:
>
> double _value;
> };
>
> class derived2 : public base
> {
> public:
> derived2() : base( 2 ), _value( 8 ) {}
>
> virtual void foo() {}
>
> private:
>
> friend boost::serialization::access;
>
> template< class ARCHIVE >
> void serialize( ARCHIVE& ar, const unsigned int version )
> {
> ar & boost::serialization::base_object<base>( *this );
> ar & _value;
> }
>
> private:
>
> short _value;
> };
>
> BOOST_CLASS_EXPORT( derived1 );
> BOOST_CLASS_EXPORT( derived2 );

Hi Christian,

it should work if you change BOOST_CLASS_EXPORT to BOOST_CLASS_EXPORT_GUID
like this:

#include <boost/serialization/export.hpp>
...
BOOST_CLASS_EXPORT_GUID(derived1, "derived1")
BOOST_CLASS_EXPORT_GUID(derived2, "derived2")

This works for me in a similar example. There is another way of
"registering" derived classes, see:
http://www.boost.org/libs/serialization/doc/serialization.html#derivedpointers

Regards,
Christian


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