Boost logo

Boost Users :

Subject: Re: [Boost-users] problem deserializing to base class pointer
From: Robert Ramey (ramey_at_[hidden])
Date: 2011-09-01 13:11:36


Shipa wrote:
> I have a strange problem.
>
> I searched through the list and modified my initial code
> quite a bit but cannot seem to get this to work. Serialization
> seems fine, but deserialization to base pointer in particular
> doesn't seem to work correctly (I am getting instance of base
> class inside).
>
> The example below is canonical version of a much more complex
> case I was working with, involving shared_ptr and template
> classes, but it boiled down (for now) to this:
>

...
> std::ofstream f2("c:\\b");
> boost::archive::text_oarchive* oa2=GetoArchiveAndRegister(f2);
> *oa2 & b;
> f2.close();
...
> //deserializes an instance of A
> std::ifstream f5("c:\\b");
> boost::archive::text_iarchive* oa5=GetiArchiveAndRegister(f5);
> *oa5 & a;
> f5.close();

...

> What am I doing wrong? Can anybody help?

Note violation of fundamental rule. You have to deserialize the same type
you serialized. Here we have:

oa << b
...
ia >> a

This cannot be expected to work. You could do the following

oa << statiic_cast<a &>(b);
...
ia >> a;

I didn't look at f8.

Robert Ramey


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