Boost logo

Boost :

From: Vladimir Prus (ghost_at_[hidden])
Date: 2004-01-21 09:42:00


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.

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

- Volodya




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