Hi,
Below is a basic sample adapted from the documentation at:
http://tinyurl.com/55fr4q
For me, with Visual Studio 2008, the deserialization of the base pointer
results in an instance of the base class, not the serialized derived
class. Also, if I add a virtual function to the base class,
deserialization results in an exception (it attempts to instantiate the
base class anyway).
I added the change cited here and rebuilt the serialization library,
which did not help:
http://tinyurl.com/5j4e8x
Any suggestions would be appreciated.
Thanks,
Steve Sanders
#include "stdafx.h"
#include "stdafx.h"
#include <sstream>
// include headers that implement a archive in simple text format
#include
#include
#include
class base {
public:
base(){ m_val = 1; }
void PrintVal( void )
{
printf(" Value: %d\n", m_val );
}
protected:
// virtual void foo(void) = 0;
friend class boost::serialization::access;
//...
// only required when using method 1 below
// no real serialization required - specify a vestigial one
template<class Archive>
void serialize(Archive & ar, const unsigned int file_version){}
int m_val;
};
class derived : public base {
public:
derived(){ m_val = 2; }
private:
void foo(void){}
friend class boost::serialization::access;
template<class Archive>
void serialize(Archive & ar, const unsigned int file_version){
// method 1 : invoke base class serialization
boost::serialization::base_object<base>(*this);
// method 2 : explicitly register base/derived relationship
boost::serialization::void_cast_register(
static_cast(NULL),
static_cast(NULL)
);
}
};
BOOST_CLASS_EXPORT_GUID(derived, "derived")
void test4(){
//...
std::stringstream ss;
boost::archive::text_oarchive oa(ss);
derived *d = new derived();
base *b = d;
b->PrintVal();
oa & d;
boost::archive::text_iarchive ar(ss);
b = NULL;
ar & b;
b->PrintVal();
}
int main() {
test4();
return 0;
}