problem deserializing to base class pointer

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:
A.h
----------------------------------------------------
#pragma once
#include "boost/serialization/serialization.hpp"
#include "boost/shared_ptr.hpp"
class A
{
public:
int t;
A():t(0){};
//friend std::ostream& operator<<(std::ostream& os, const A& a);
virtual ~A(){ };
friend class boost::serialization::access;
template<class Archive>
void serialize(Archive & ar, const unsigned int version)
{
ar & t;
}
};
std::ostream& operator<<(std::ostream& os, const A& a)
{
return os << "A: t = " << a.t << std::endl;
}
typedef boost::shared_ptr<A> APtr;
----------------------------------------------------
B.h
----------------------------------------------------
#pragma once
#include "boost/serialization/serialization.hpp"
#include "boost/shared_ptr.hpp"
class B: public A
{
public:
int m;
B():A(),m(0){};
~B(){};
//friend std::ostream& operator<<(std::ostream& os, const B& b);
friend class boost::serialization::access;
template<class Archive>
void serialize(Archive & ar, const unsigned int version)
{
ar & boost::serialization::base_object<A>(*this);
ar & m;
}
};
std::ostream& operator<<(std::ostream& os, const B& b)
{
return os << "B: t = "<< b.t << "; m = "<< b.m << std::endl;
}
typedef boost::shared_ptr<B> BPtr;
----------------------------------------------------------------
Test.cpp
----------------------------------------------------------------
#include "stdafx.h"
#include "boost/shared_ptr.hpp"
#include "boost/serialization/shared_ptr.hpp"
#include

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(b); ... ia >> a; I didn't look at f8. Robert Ramey
participants (2)
-
Robert Ramey
-
Shipa