Serialize a class, call new() in constructor
Hi everybody, actually i have a big problem with serializing some of my classes. The problem is, that classA class calls new of classB in constructor and after that the load_construct_data is called and also calls a new of classB. So I have two objects of classB. Here is my code: classA.h: class CClassA { template<class Archive> friend void serialize(Archive &ar, CClassA &cCClassA, const unsigned int version); template<class Archive> friend void save_construct_data(Archive &ar, const CClassA *cCClassA, const unsigned int version); template<class Archive> friend void load_construct_data(Archive &ar, CClassA *cCClassA, const unsigned int version); // *** Aufzählungen **************************************************************************** public: CClassB* pClassB; // *** Methoden ******************************************************************************** private: protected: int iTest; public: CClassA(int iTest); // Konstruktor virtual ~CClassA(); // Destruktor // Get/Set-Methoden }; classA.cpp: CClassA::CClassA(int iTest) :iTest(iTest) { pClassB = new CClassB(iTest); } classB.h: class CClassB { template<class Archive> friend void serialize(Archive &ar, CClassB &cCClassB, const unsigned int version); template<class Archive> friend void save_construct_data(Archive &ar, const CClassB *cCClassB, const unsigned int version); template<class Archive> friend void load_construct_data(Archive &ar, CClassB *cCClassB, const unsigned int version); // *** Aufzählungen **************************************************************************** public: // *** Datenmember ***************************************************************************** private: protected: public: // *** Methoden ******************************************************************************** private: protected: int iTestInt; public: CClassB(int iTest); // Konstruktor virtual ~CClassB(); // Destruktor // Get/Set-Methoden static unsigned int count; }; serialize.h template<class Archive> void serialize(Archive &ar, CClassA &cCClassA, const unsigned int version) { ar & boost::serialization::make_nvp("ClassB", cCClassA.pClassB ); } template<class Archive> void save_construct_data(Archive &ar, const CClassA *cCClassA, const unsigned int version) { ar << boost::serialization::make_nvp("Test", cCClassA->iTest); } template<class Archive> void load_construct_data(Archive &ar, CClassA *cCClassA, const unsigned int version) { int iTest; ar >> boost::serialization::make_nvp("Test", iTest); ::new(cCClassA) CClassA(iTest); } template<class Archive> void serialize(Archive &ar, CClassB &cCClassB, const unsigned int version) { } template<class Archive> void save_construct_data(Archive &ar, const CClassB *cCClassB, const unsigned int version) { ar << boost::serialization::make_nvp("Test", cCClassB->iTestInt); } template<class Archive> void load_construct_data(Archive &ar, CClassB *cCClassB, const unsigned int version) { int iTest; ar >> boost::serialization::make_nvp("Test", iTest); ::new(cCClassB) CClassB(iTest); } Now I just want that only one ClassB will be created. How can I recognize, that ClassB is already generate through cons tructor classA. I just want to serialize the members of classB. Is there a way to solve my problem?? Best regards Michael
Michael Reichlin wrote:
Hi everybody,
actually i have a big problem with serializing some of my classes. The problem is, that classA class calls new of classB in constructor and after that the load_construct_data is called and also calls a new of classB. So I have two objects of classB.
serialize.h template<class Archive> void serialize(Archive &ar, CClassA &cCClassA, const unsigned int version) {
// Just comment out the following- don't serialize B
ar & boost::serialization::make_nvp("ClassB", cCClassA.pClassB );
//
}
template<class Archive> void save_construct_data(Archive &ar, const CClassA *cCClassA, const unsigned int version) { ar << boost::serialization::make_nvp("Test", cCClassA->iTest); } template<class Archive> void load_construct_data(Archive &ar, CClassA *cCClassA, const unsigned int version) { int iTest; ar >> boost::serialization::make_nvp("Test", iTest); ::new(cCClassA) CClassA(iTest); }
Now I just want that only one ClassB will be created. How can I recognize, that ClassB is already generate through cons tructor classA. I just want to serialize the members of classB.
Is there a way to solve my problem??
participants (2)
-
Michael Reichlin
-
Robert Ramey