#include #include #include #include #include #include #include /* ClassHaha is a class( with a member 'mem') which appears as a member in other classes . The abstract base class used is gps_position which has a pointer 'ptr' to object of ClassHaha . There are two objects derived from class gps_position one is gps_moreAcc1 which also has an additional pointer ptr1 to ClassHaha and the other derived class is gps_moreAcc2 which also has an additional member as a pointer to object of ClassHaha . I create 2 objects g1 and g2 i.e one each of classes gps_moreAcc1 and gps_moreAcc2 . Then I create 3 objects 1,2,3 of class ClassHaha( by settings its mem field ) all in the heap and then the pointer 'ptr' of object g1 and g2 is set to the same object '1' of ClassHaha , then memeber 'ptr1' of g1 is pointed to object '2' of ClassHaha , then 'ptr2' of g1 is pointed to object '3' of ClassHaha . After writing , while restoring , 'ptr' of g1 points to object '1' of ClassHaha and 'ptr1' points to object '1' of ClassHaha as expected . But the problem appears with g2 object whose 'ptr' points to object '2' of ClassHaha instead of object '1' of ClassHaha . */ class ClassHaha { private: friend class boost::serialization::access; template void serialize(Archive & ar, const unsigned int version) { ar & mem; } public: double mem; ClassHaha() { mem = 0 ; std::cout<<"\n Constructing 1 ClassHaha\n";} ClassHaha( double d) { mem = d; std::cout<<"\n Constructing 2 ClassHaha\n";}; }; BOOST_CLASS_TRACKING(ClassHaha, boost::serialization::track_always) class gps_position { private: friend class boost::serialization::access; template void serialize(Archive & ar, const unsigned int version) { ar & degrees; ar & ptr; } public: int degrees; ClassHaha *ptr; gps_position(){ std::cout<< "\nConstructing 1 gps\n" ;degrees = 0 ;}; gps_position(int d) : degrees(d) { std::cout<< "\nConstructing 2 gps\n" ; } virtual void display() = 0; void setHaha( ClassHaha *newHaha); }; BOOST_IS_ABSTRACT(gps_position) void gps_position::setHaha( ClassHaha *newHaha) { ptr = newHaha; } class gps_moreAcc1 : public gps_position { private: friend class boost::serialization::access; template void serialize(Archive & ar, const unsigned int version) { ar & boost::serialization::base_object(*this); ar & milli; ar & ptr1; } float milli; ClassHaha *ptr1; public: gps_moreAcc1() { std::cout<<"Constructing 1 gps_moreAcc1"; milli = 0; } gps_moreAcc1( int d, float x): gps_position( d), milli(x){ std::cout<<"Constructing 2 gps_moreAcc1" ;} void setMyHaha( ClassHaha *newHaha); void display(); }; // BOOST_CLASS_EXPORT( gps_moreAcc1) void gps_moreAcc1::setMyHaha( ClassHaha *newHaha) { ptr1 = newHaha; } void gps_moreAcc1::display() { std::cout <<"\n1 Degrees:"<< degrees <<" ,ptr->mem:"<< ptr->mem <<" ,miLli:"<< milli << " , ptr1->mem:"<< ptr1->mem; } class gps_moreAcc2 : public gps_position { private: friend class boost::serialization::access; template void serialize(Archive & ar, const unsigned int version) { ar & boost::serialization::base_object(*this); ar & micro; ar & ptr2; } float micro; ClassHaha *ptr2; public: gps_moreAcc2() { std::cout<<"Constructing 1 gps_moreAcc2"; micro = 0;} gps_moreAcc2( int d, float x) : gps_position( d), micro(x){ std::cout<<"Constructing 2 gps_moreAcc2";} void setMyHaha( ClassHaha *newHaha); void display(); }; // BOOST_CLASS_EXPORT( gps_moreAcc2) void gps_moreAcc2::setMyHaha( ClassHaha *newHaha) { ptr2 = newHaha; } void gps_moreAcc2::display() { std::cout <<"\n2 Degrees:"<< degrees <<" ,ptr->mem:"<< ptr->mem <<" ,micro:"<< micro << " , ptr2->mem:"<< ptr2->mem; } class test_gps { private: friend class boost::serialization::access; template void serialize(Archive & ar, const unsigned int version) { ar.register_type(static_cast(NULL)); ar.register_type(static_cast(NULL)); ar & pos1; ar & pos2; } gps_position *pos1; // these are pointers to base object so that if it points to the derived object read/write must take place for the derived objects , this can be validated at restore gps_position *pos2; public: test_gps() { pos1 = NULL ; pos2 = NULL;}; void setvalue( gps_position *x, gps_position *y); void display(); }; void test_gps::setvalue( gps_position *x, gps_position *y) { pos1 = x; pos2 = y; } void test_gps::display() { pos1->display(); pos2->display(); } void save_gps(const test_gps &s) { // create and open a character archive for output std::ofstream ofs("filename.txt"); boost::archive::text_oarchive oa(ofs); oa << s; } int main() { // create class instance gps_moreAcc1 *g1 = new gps_moreAcc1(35, 1.1f); gps_moreAcc2 *g2 = new gps_moreAcc2(351, 2.2f); ClassHaha *newDoub = new ClassHaha(1); g1->setHaha( newDoub); // make members of g1 and g2 point to the same object g2->setHaha( newDoub); newDoub = new ClassHaha(2); g1->setMyHaha( newDoub); newDoub = new ClassHaha(3); g2->setMyHaha( newDoub); test_gps *gT = new test_gps(); gT->setvalue( g1, g2); save_gps( *gT); gT->display(); std::cout<<" \nDone writing "; test_gps *newg ; { // create and open an archive for input std::ifstream ifs("filename.txt", std::ios::binary); boost::archive::text_iarchive ia(ifs); // read class state from archive ia >> newg; // archive and stream closed when destructors are called } newg->display(); return 0; }