I've got a system where class A { A(); A(const string&); virtual ~A(); }; class C : public A { C(); C(const A&, float); virtual ~C(); }; BOOST_CLASS_EXPORT( A ) BOOST_CLASS_EXPORT( C ) main() { ... const A* a = new a("stuff"); const C* c = new c(*a, 52.54390f); { ofstream ofs("dump.bin"); boost::archive::text_oarchive oa(ofs); oa<<a<<c; } ... A* a2=NULL; C* c2=NULL; try { ifstream ifs("dump.bin"); boost::archive::text_iarchive ia(ifs); ia>>a2; ia>>c2; } catch(boost::archive::archive_exception& ae) { cout<<ae.what(); } } This works just fine. If I replace A* a2=NULL; C* c2=NULL; with A* a2=NULL; A* c2=NULL; Then it throws an unregistered_class exception. If I change the iarchive to take: const A* a = new A("I'm cool"); const A* c = new C(*a,54.2f); then I can unarchive it with a pair of A*. Is this by design? Seriously? I can't save derived pointers and unarchive base pointers? Larry
Hi, Why do you think that this should work? I would assume that serialisation dos only make sense if serialization and reconstruction are symmetric. This cas does not look very symmetric to me. On 2012-10-23 19:31, Larry E. Ramey wrote:
Is this by design? Seriously? I can't save derived pointers and unarchive base pointers?
Larry
Larry E. Ramey wrote:
Is this by design?
yes
Seriously?
yes
I can't save derived pointers and unarchive base pointers?
no the extra information indicating what kind of pointer is is only saved when necessary. That is when the saving is done through a base class pointer. In general all saving/loading must be between the same types. Seriously. Robert Ramey
Larry
I split my test program up from 1 .cpp file into proper headers and implementation files.... (we'll fight the fight with dlls next) I now get a unregisted_class exception when I do this: (MSVC 11.0, windows7, win32 compile) int main(int argc, char* argv[]) { const ABase* a = new ABase("I'm cool"); const ABase* c = new C(*a,54.2f); cout <<*a<<"\n"<<*((C*)c)<<endl; ofstream ofs("dump.bin"); try { boost::archive::text_oarchive oa(ofs); //boost::archive::binary_oarchive oa(ofs); oa<<a; oa<<c; //THIS TRIGGERS THE EXCEPTION } catch(boost::archive::archive_exception& ae) { cout<<ae.what(); } Where C inherets from ABase. C.h: #ifndef C_HPP #define C_HPP #include "A.h" class C : public ABase { friend class boost::serialization::access; friend std::ostream& operator << (std::ostream&,const C&); public: C(); C(const ABase&); C(const ABase&,float); virtual ~C(); private: template<class Archive> void serialize(Archive & ar, const unsigned int version) { ar & boost::serialization::base_object<ABase>(*this); ar & m_blah; } float m_blah; }; BOOST_CLASS_EXPORT_KEY( C ) #endif c.cpp: #include "C.h" using namespace std; BOOST_CLASS_EXPORT_IMPLEMENT( C ) C::C() { } C::C(const ABase&a,float b): ABase(a), m_blah(b) { } C::~C() { } ostream& operator << ( ostream& out, const C& rhs) { out << dynamic_cast<const ABase&>(rhs) <<" " <<rhs.m_blah <<endl; return out; } I do not for the life of me understand. I am registering the classes like the documentation says.... Larry E. Ramey
participants (3)
-
Larry E. Ramey
-
Oswin Krause
-
Robert Ramey