#include #include #include #include struct MyInt { MyInt():i_(0) {} MyInt(int i):i_(i) {} int i_; friend class boost::serialization::access; template void serialize(Archive &ar, unsigned int const /* file_version */) { ar & BOOST_SERIALIZATION_NVP(i_); } }; struct Foo { MyInt& ref_; Foo(MyInt& ref):ref_(ref) {} friend class boost::serialization::access; template void serialize(Archive &ar, unsigned int const /* file_version */) {} template friend void save_construct_data(Archive& ar, Foo const* p, unsigned int const /* file_version */); template void load_construct_data(Archive& ar, Foo* p, unsigned int const /* file_version */); }; template void save_construct_data(Archive& ar, Foo const* p, unsigned int const /* file_version */) { #if 0 // OK MyInt* pRef = &p->ref_; ar << boost::serialization::make_nvp("ref_", pRef); #else // Error ar << boost::serialization::make_nvp("ref_", &p->ref_); #endif } template void load_construct_data(Archive & ar, Foo* p, unsigned int const /* file_version */) { MyInt* pRef; ar >> boost::serialization::make_nvp("ref_", pRef); ::new(p) Foo(*pRef); } struct Bar { Bar():mi_(1) {} MyInt mi_; Foo* pFoo_; friend class boost::serialization::access; template void serialize(Archive &ar, unsigned int const /* file_version */) { ar & BOOST_SERIALIZATION_NVP(mi_); ar & BOOST_SERIALIZATION_NVP(pFoo_); } }; int main() { { std::ofstream ofs("ref.xml"); Bar b1; b1.pFoo_ = new Foo (b1.mi_); b1.pFoo_->ref_.i_ = 2; assert(b1.mi_.i_ == 2); boost::archive::xml_oarchive oa(ofs); oa << BOOST_SERIALIZATION_NVP(b1); delete b1.pFoo_; } { std::ifstream ifs("ref.xml"); boost::archive::xml_iarchive ia(ifs); Bar b2; ia >> BOOST_SERIALIZATION_NVP(b2); assert(b2.mi_.i_ == 2); assert(b2.pFoo_->ref_.i_ == 2); b2.mi_.i_ = 3; assert(b2.pFoo_->ref_.i_ == 3); } }