the base class MUST be polymorphic - that it it must have at least one virtual function. A practical suggestion would be to make at least one of those functions abstract ( a() = 0) so that you can't accidently "slice" off the derived class. Robert Ramey John Yamokoski wrote:
Yea I have done that. I have read the sections on exporting several times and thought my example was implementing this feature correctly. Yet every time I run it, the code only spits out the base class. The only complete example from the documentation shows the explicit declaration of derived class names. Once again, here is my test code:
main.cpp:
class base { friend class boost::serialization::access; template<class Archive> void serialize(Archive & ar, const unsigned int file_version) { ar & BOOST_SERIALIZATION_NVP(dval); } public: base() : dval(0.0) {} base(double b) : dval(b) {} double dval; };
class derived : public base { friend class boost::serialization::access; template<class Archive> void serialize(Archive & ar, const unsigned int file_version) { boost::serialization::void_cast_register
(static_cast (NULL),static_cast (NULL)); ar & BOOST_SERIALIZATION_NVP(ival); } public: derived() : base(0.0), ival(0) {} derived(int a, double b) : base(b), ival(a) {} int ival; }; BOOST_CLASS_EXPORT_GUID(derived, "derived");
class manager { friend class boost::serialization::access; template<class Archive> void serialize(Archive & ar, const unsigned int file_version) { ar & BOOST_SERIALIZATION_NVP(objs); } public: std::list
objs; }; void SerializeTest::testSerialize() { manager mgr;
// Creat some data to save derived *d1, *d2; d1 = new derived(1, 1.0); d2 = new derived(2, 3.0);
mgr.objs.push_back( d1 ); mgr.objs.push_back( d2 );
{ std::ofstream ofs("out.xml"); assert(ofs.good()); boost::archive::xml_oarchive oa(ofs); oa << BOOST_SERIALIZATION_NVP(mgr); ofs.close(); }
manager new_mgr;
{ std::ifstream ifs("out.xml"); assert(ifs.good()); boost::archive::xml_iarchive ia(ifs); ia >> BOOST_SERIALIZATION_NVP(new_mgr); ifs.close(); }
{ std::ofstream ofs("check.xml"); assert(ofs.good()); boost::archive::xml_oarchive oa(ofs); oa << BOOST_SERIALIZATION_NVP(new_mgr); ofs.close(); }
delete d1, d2; }
And here is the output of out.xml:
<?xml version="1.0" encoding="UTF-8" standalone="yes" ?> <!DOCTYPE boost_serialization>
<mgr class_id="0" tracking_level="0" version="0"> <objs class_id="1" tracking_level="0" version="0"> <count>2</count> 0 <item class_id="2" tracking_level="1" version="0" object_id="_0"> <dval>1</dval> </item> <item class_id_reference="2" object_id="_1"> <dval>3</dval> </item> </objs> </mgr>On Tue, Sep 16, 2008 at 6:32 PM, Robert Ramey
wrote: Look in the documentation of "export" and BOOST_CLASS_EXPORT