|
Boost Users : |
From: Robert Ramey (ramey_at_[hidden])
Date: 2007-04-18 22:26:41
I took your code and fixed it up to that below and it compile and tests
fine.
Note the usage of BOOST_SERIALIZATION_BASE_OBJECT_NVP
below which is key. This is explained in the documentation.
Robert Ramey
Luca Cappa wrote:
#include <boost/serialization/shared_ptr.hpp>
#include <boost/archive/xml_oarchive.hpp>
#include <fstream>
using namespace std;
using namespace boost;
using namespace boost::serialization;
struct SerializableBase
{
virtual ~SerializableBase ()
{
}
template<typename Archive>
void serialize (Archive& pAR, unsigned int version)
{
int i (0);
pAR & make_nvp ("doesnotwork", i);
}
};
struct Serializable : public SerializableBase
{
int a;
Serializable () : a (1)
{
}
virtual ~Serializable ()
{
}
template<typename Archive>
void serialize (Archive& pAR, unsigned int version)
{
pAR & BOOST_SERIALIZATION_BASE_OBJECT_NVP(SerializableBase);
pAR & BOOST_SERIALIZATION_NVP (a);
}
};//struct
void save (const Serializable& pS, const char* pFileName)
{
// make an archive
std::ofstream ofs (pFileName);
boost::archive::xml_oarchive oa (ofs);
// Registering a lot of things here!
oa.register_type (static_cast<Serializable*> (NULL));
SerializableBase* lS (new Serializable);
oa << make_nvp ("lS", lS);
}
int main ()
{
Serializable s;
save (s, "serializable.xml");
}
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