Darryl Lawson:
I realise that what I am doing is no doubt an unconventional usage of boost::serialization, but I wonder if any one has done a similar thing before.
I wish to serialize a derived class through a baseclass pointer, but on deserilize I do not want the object to be created (I do not want memory allocated) - as the object will have all ready been created.
An example, hopefully showing what I mean, is given at the end.
...
struct A { A() { _m = new derived; cout << "construct A, _m = " << (unsigned long)_m << endl; }
template<class Archive> void serialize(Archive& ar, const unsigned int version) { // On deserialize this will create a new object, but I do not want it to, // rather I would like it to deserialize into the all ready created // object. ar & _m; cout << "serialise A, _m = " << (unsigned long)_m << endl; }
base* _m; };
void save(const A& a, const char* filename) { std::ofstream ofs(filename); boost::archive::text_oarchive oa(ofs); oa << a; }
void load(A& a, const char* filename) { std::ifstream ifs(filename); boost::archive::text_iarchive ia(ifs); ia >> a; }
You can add virtual void load( boost::archive::text_iarchive& ar, unsigned v ) = 0; virtual void save( boost::archive::text_oarchive& ar, unsigned v ) = 0; to base and call them in A::serialize, but this obviously only works for a specific archive type.