我有三類:升壓轉換器的load_construct_data並通過指針序列化派生類基礎
class A
{
private:
friend class boost::serialization::access;
template<class Archive>
void serialize(Archive & ar, const unsigned int version)
{
ar & this->id & this->somefield;
}
protected:
A() {} // just for boost, normally I create this class with non-default constructor
int id;
Sometype somefield;
public:
A(unsigned int id);
// blah blah, also some virtual methods
};
// _____________________ CLASS B
class B : public A
{
private:
friend class boost::serialization::access;
template<class Archive> inline friend void load_construct_data(Archive &ar, B *t, const unsigned int file_version);
template<class Archive> inline friend void save_construct_data(Archive &ar, const B *t, const unsigned int file_version);
B(unsigned int id, Sometype somefield, Sometype some_new_field1, Sometype some_new_field2);
Sometype some_new_field1;
Sometype some_new_field2;
template<class Archive>
void serialize(Archive & ar, const unsigned int version) {
ar & boost::serialization::base_object<A>(*this);
}
public:
// blah blah, also virtual methods
};
template<class Archive>
inline void save_construct_data(
Archive & ar, const B * t, const unsigned int file_version
){
ar << t->id << t->somefield << t->some_new_field1 << t->some_new_field2;
}
template<class Archive>
inline void load_construct_data(
Archive & ar, B * t, const unsigned int file_version
){
unsigned int _id;
Sometype _somefield;
Sometype _some_new_field1;
Sometype _some_new_field2;
ar >> _id >> _somefield >> _some_new_field1 >> _some_new_field2;
::new(t)B(_id, _somefield, _some_new_field1, _some_new_field2);
}
// _____________________ CLASS C
class C : public A
{
private:
friend class boost::serialization::access;
template<class Archive> inline friend void load_construct_data(Archive &ar, C *t, const unsigned int file_version);
template<class Archive> inline friend void save_construct_data(Archive &ar, const C *t, const unsigned int file_version);
C(unsigned int id, Sometype somefield, Sometype some_new_field3, Sometype some_new_field4);
Sometype some_new_field3;
Sometype some_new_field4;
template<class Archive>
void serialize(Archive & ar, const unsigned int version) {
ar & boost::serialization::base_object<A>(*this);
}
public:
// blah blah, also virtual methods
};
template<class Archive>
inline void save_construct_data(
Archive & ar, const C * t, const unsigned int file_version
){
ar << t->id << t->somefield << t->some_new_field3 << t->some_new_field4;
}
template<class Archive>
inline void load_construct_data(
Archive & ar, C * t, const unsigned int file_version
){
unsigned int _id;
Sometype _somefield;
Sometype _some_new_field3;
Sometype _some_new_field4;
ar >> _id >> _somefield >> _some_new_field3 >> _some_new_field4;
::new(t)C(_id, _somefield, _some_new_field3, _some_new_field4);
}
我需要使用非默認的構造函數,所以我需要加載/ save_construct_data覆蓋。 B和C類是通過向量序列化和反序列化的,因此我使用宏BOOST_CLASS_EXPORT()(在另一部分代碼中,我認爲不相關)。我的問題是,當我反序列化使用上面的代碼創建的存檔時,我的向量中會有重複的指針(每個指針指向內存中的同一區域) - 它可能是一個用於基礎的派生類,另一個用於派生類。我找不到任何地方,當通過指向基類的指針保存我的對象時,如何使用load/save_construct數據,而無需重複。我花在這個星期,我的最後期限很快來臨之際,任何幫助非常感謝:)
編輯:我要提一件事:當我刪除從基類的「序列化」方法的內容(僅適用於內容,而不是方法本身),在反序列化之後,我什麼也得不到,當我用「base_object」刪除部分時,archive會拋出一個異常「未註冊的void cast」。