As in the question title – I need o serialize a complex net of data structures pointing at each oher and are represented by classes that may have no default constructors. The classes usually have a vector of pointers or vector of pairs .
I thought it would make sense to simply allocate the memory of the sizeof(SomeClass), then fill it with the serialization routine, but this somehow causes uninitialized memory problems associated with strings (resize() failing, probably uninitialized string object is not willing to work). The example code looks like this:
friend class boost::serialization::access;
template<class Archive> void save(Archive & ar, const unsigned int version) const
{
int n;
ar & vec.size();
for(int i=0; i<n; i++)
{
ar & vec[i];
}
}
template<class Archive> void load(Archive & ar, const unsigned int version) const
{
int n;
ar & n;
for(int i=0; i<n; i++)
{
SomeClass* obj = (SomeClass*)::operator new(sizeof(SomeClass));
ar & *obj;
vec.push_back(*obj);
}
}
This is covered in the following section of the boost::serialization documentation:
boost::serialization – Non-Default Constructors
Basically you need to write your own versions of
load_construct_dataandsave_construct_data