void_cast_register should be necessary only in the most unusual cases. It
should happen automatically as part of the "base_object" wrappers call.
Also it is used void_cast_register
I am using Boost 1.32.0 with Borland C++ Builder 6 and attempting to serialize an object via a pointer to a base class but I am getting a runtime exception of "unregistered void cast".
Having read the page in the documentation on Runtime Casting, I thought I should be able to use boost::serialization::void_cast_register
(0,0); to associate the base and derived classes, but I can't get it to work. If I use the alternate method (to serialize the base class and provide a do-nothing serialize method in the base class) then it works fine. Any ideas what's wrong? The program below demonstrates the problem. Class A uses the serialize base class method and that's ok while class B uses the void_cast_register method and an exception is thrown when it's serialized: ----- #include <iostream> #include
#include #include #include #include #if defined(_DEBUG) # pragma link "libboost_serialization-bcb-mt-d-1_32.lib" #else # pragma link "libboost_serialization-bcb-mt-1_32.lib" #endif
class Base_c { public: virtual void SomeMethod() = 0; private: friend class boost::serialization::access; template<class Archive> void serialize(Archive & ar, const unsigned int version) {} }; BOOST_IS_ABSTRACT(Base_c)
class A : public Base_c { public: A() : m_Id("A"){}; void SomeMethod(){}; private: friend class boost::serialization::access; template<class Archive> void serialize(Archive & ar, const unsigned int version) { /* boost::serialization::void_cast_register
(0,0); */ ar & boost::serialization::make_nvp("Base", boost::serialization::base_object (*this)); ar & BOOST_SERIALIZATION_NVP(m_Id); } std::string const m_Id; }; BOOST_CLASS_EXPORT(A); class B : public Base_c { public: B() : m_Id("B"){}; void SomeMethod(){};
private: friend class boost::serialization::access; template<class Archive> void serialize(Archive & ar, const unsigned int version) { boost::serialization::void_cast_register
(0,0); /* ar & boost::serialization::make_nvp("Base", boost::serialization::base_object (*this)); */ ar & BOOST_SERIALIZATION_NVP(m_Id); } std::string const m_Id; }; BOOST_CLASS_EXPORT(B) main(int, char**) {
boost::filesystem::ofstream OfStream(boost::filesystem::path("D:\\Temp\\SerialisationText.xml", boost::filesystem::native)); boost::archive::xml_oarchive Archive(OfStream);
try { Base_c *pBase, *pBase2; pBase = new A(); pBase2 = new B();
Archive & boost::serialization::make_nvp("AByBasePointer", pBase); std::cout << "Serialised A ok\n"; Archive & boost::serialization::make_nvp("BByBasePointer", pBase2); std::cout << "Serialised B ok\n"; } catch (std::exception& E) { std::cout << "Exception: " << E.what(); } } ----- The output is: Serialised A ok Exception: unregistered void cast