我正在尝试序列化一个派生类,其中base没有默认构造函数。到目前为止我所做的是:
struct Data {
int _value;
};
class Base;
namespace boost { namespace serialization {
template<typename Archive>
void save_construct_data(Archive&, const Base*, unsigned int);
template<typename Archive>
void load_construct_data(Archive&, Base*, unsigned int);
}}
class Base {
Data& _ref;
template<typename Archive>
void serialize(Archive&, unsigned int)
{}
friend class boost::serialization::access;
//friend load/save construct data, etc.
public:
Base(Data& ref) : _ref(ref) {}
};
BOOST_CLASS_TRACKING(Base, boost::serialization::track_always);
//implementation of load/save construct data, etc
class Derived : public virtual Base {
template<typename Archive>
void serialize(Archive& ar, unsigned int)
{
//save / load construct data NOT CALLED!
ar & BOOST_SERIALIZATION_BASE_OBJECT_NVP(Base);
}
//other methods, etc
};
上面,调用Base的serialize()方法,它什么都不做!有没有办法从派生类调用save_construct_data或load_construct_data函数(不绕过序列化机制)?本练习的重点是跟踪Base的内存位置。
提前谢谢。