the following is the way to handle const reference members, which are initialized in the constructor. Serialization seems to be working, from
an inspection of the resulting XML file, but after deserialization, the values in the object given by the const reference are wrong and are
all zero.
Here is my question: Is load_construct_data() below correct? It looks like it is initializing the constant reference from a temporary, but
I'm doing the same thing as the example in the documentation.
class MyClass1 {
friend boost::serialization::access;
…
};
class MyClass2 {
friend boost::serialization::access;
const MyClass1 & mc1_;
MyClass2 ( const MyClass1 &mc1) : mc1_(mc1) { } ;
};
namespace boost { namespace serialization {
template<class Archive>
inline void save_construct_data(
Archive &ar, const MyClass2 *t, const unsigned int file_version)
{
// mc1_ is a reference and requires special handling -- save it
// as a pointer
const MyClass1 *mc1 = &t->mc1_ ;
ar << boost::serialization::make_nvp("mc1_", mc1);
}
template<class Archive>
inline void load_construct_data(
Archive &ar, MyClass2* t, const unsigned int file_version)
{
MyClass1 *mc1;
ar >> boost::serialization::make_nvp("mc1_", mc1);
// NOTE: IS THIS INITIALIZING THE REFERENCE FROM A TEMPORARY?
::new(t) MyClass2(*mc1);
}
Thanks,
Kurt