#include #include #include #include #include #include #include class B { public: B() : i(10) {} virtual void foo() const {} void save(boost::basic_oarchive& ar) const { ar << i; } void load(boost::basic_iarchive& ar, boost::version_type) { ar >> i; } static boost::version_type version() { return 0; } private: int i; }; class D1 : public B { public: D1() {} virtual void foo() const {} void save(boost::basic_oarchive& ar) const { ar << static_cast(*this); } void load(boost::basic_iarchive& ar, boost::version_type) { ar >> static_cast(*this); } static boost::version_type version() { return 0; } }; class D2 : public B { public: D2() {} virtual void foo() const {} void save(boost::basic_oarchive& ar) const { ar << static_cast(*this); } void load(boost::basic_iarchive& ar, boost::version_type) { ar >> static_cast(*this); } static boost::version_type version() { return 1; } }; int main() { B b; B* d1 = new D1; B* d2 = new D2; try { std::string s("sep"); // Suppose program 1 writes the following strstream ss1; boost::oarchive ar(ss1); ar << static_cast(0) << static_cast(0); ar << s; ar << d1 << d2; ss1 << '\0'; cout << ss1.str() << endl; cout << "************\n"; // Suppose program 2 writes the following strstream ss2; boost::oarchive ar2(ss2); ar2 << static_cast(0) << static_cast(0); ar2 << s; ar2 << d1 << d2; ss2 << '\0'; cout << ss2.str() << endl; // Suppose program 3 needs to read output of programs 1 and 2. boost::iarchive in1(ss1); D1* d1; D2* d2; B* rd1; B* rd2; in1 >> d2 >> d1 >> s >> rd1 >> rd2; // Here rd1 will be D1 and rd2 will be D2, as it should be cout << typeid(*rd1).name() << ", " << typeid(*rd2).name() << endl; boost::iarchive in2(ss2); in2 >> d2 >> d1 >> s >> rd1 >> rd2; // Here rd1 will be D2 and rd2 will be D1, contrary to what is written. cout << typeid(*rd1).name() << ", " << typeid(*rd2).name() << endl; } catch(boost::archive_exception& e) { cout << "archive exception.\n"; } return 0; }