Boost logo

Boost Users :

Subject: Re: [Boost-users] Boost.Serialization: Restoring object state(preventcreation of a new object)?
From: ray_at_[hidden]
Date: 2010-08-31 02:17:52


> That's likely something else entirely. We'd have to see more code. Forget my previous post, the exception was due to mixed up ofstream and ifstream... Just to resume - if I have a list of pointers to the objects (all derived from the same base class) and I'd like to serialize them through a reference, a code like this would be needed: // List of pointers A a; B b; // Derived from A C c; // Derived from C std::list<A*> list; list.push_back(&a); // Base list.push_back(&b); // Derived list.push_back(&c); // Derived ... // Save std::ofstream ofs("c:/test.xml"); boost::archive::xml_oarchive oa(ofs); BOOST_FOREACH(A* pA, list) { if (dynamic_cast<B*>(pA)) { B& b = *static_cast<B*>(pA); oa & BOOST_SERIALIZATION_NVP(b); } else if (dynamic_cast<C*>(pA)) { C& c = *pA; BOOST_SERIALIZATION_NVP(c); } else { A& a = *pA; BOOST_SERIALIZATION_NVP(a); } } Am I right?



Boost-users list run by williamkempf at hotmail.com, kalb at libertysoft.com, bjorn.karlsson at readsoft.com, gregod at cs.rpi.edu, wekempf at cox.net