Boost logo

Boost Users :

From: Robert Ramey (ramey_at_[hidden])
Date: 2006-10-23 12:20:06


For this two work, the base class MUST be polymorphic.
To fix this make one of your functions "virtual". A simple
and common way to do this is to make the destructor
virtual.

Another good idea in my opinion is to make the
base class abstract by setting all functions "=0".
(you'll have to use BOOST_IS_ABSTRACT
as well). This is not required for serializaton.
But I believe it helps clarify the design pattern
where the base class is an interface while the
derived classes are specific implementations.
In this case I find it helpful to make the constructors
(and destructor) "protected:") as well.

Robert Ramey

Christian Henning wrote:
> Hi there, I'm having problems when serializing derived classes from a
> non-polymorphic base class. I have read the section "Pointers to
> Objects of Derived Classes". But for some reasons I cannot make these
> tricks to work in my application. I have created a small sample
> application that shows my problem:
>
> class base
> {
> public:
> base() : _type( 0 ) {}
> base( unsigned int type) : _type( type ) {}
>
> unsigned int type() { return _type; }
> private:
>
> friend boost::serialization::access;
>
> template< class ARCHIVE >
> void serialize( ARCHIVE& ar, const unsigned int version )
> {
> ar & _type;
> }
>
> private:
>
> unsigned int _type;
> };
>
> class derived1 : public base
> {
> public:
> derived1() : base( 1 ), _value( 2.1 ) {}
>
> 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 ) {}
>
> 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 );
>
> int _tmain(int argc, _TCHAR* argv[])
> {
> std::string data;
>
> {
> std::ostringstream archive_stream;
> boost::archive::binary_oarchive archive( archive_stream );
>
> derived2 d;
> archive & d;
>
> data = archive_stream.str();
> }
>
> {
> // deserialize
> std::string archive_data( &data[0], data.size() );
> std::istringstream archive_stream( archive_data );
> boost::archive::binary_iarchive archive( archive_stream );
>
> // I don't know what derived class was serialized.
> base* b;
> archive & b;
>
> if( b->type() == 1 )
> {
> derived1* d1 = static_cast<derived1*>( b );
> }
> else
> {
> derived2* d2 = static_cast<derived2*>( b );
> }
> }
>
> return 0;
> }
>
> Any help is more than welcome.
>
> Thanks ahead,
> 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