Boost logo

Boost :

From: Robert Ramey (ramey_at_[hidden])
Date: 2004-01-21 12:42:40


Vladimir Prus wrote:

>I've just spend about an hour with a colleague looking for a very subtle
>problem with serialization and BOOST_CLASS_EXPORT.

>What we were trying to do is to have some code which reads pointers to base
>class and does something with it. On the saving side, we've got several
>functions which store various derived classes.

>Now the example:

> class B { ... } ;
> class D { ... } ;
> BOOST_CLASS_EXPORT(D);

> D* d = new D;
> oa << d;

>Looks innocent, but it does not store the name of derived class that is
>stored. So, after loading with

> B* b;
> is >> b;

>you get object of type B, not D. I attach the program which reproduces this
>problem.

>The gotcha is that if you write

> B* d = new D;
> oa << d;

>everything works. But it's nonintuitive, and the mistake is very easy to make
>and very hard to notice! In my case, one function was intended for saving
>specific derived type, so it naturally declared a pointer of that type, and
>it took an hour to debug the problem.

Only an hour?

>Is it possible to always save proper identification when saving a pointer to
>polymorphic objects?

This looks like an error to me. The intention is that it work as you expected.
I'll look into it.

Robert Ramey


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