Serialization when using virtual inheritance
Boost version: 1.32.0 I am trying to use the serialization class to serialize data pointed to by the virtual base class. Note that the code works great if I don't use virtual inheritance off of the base class. Here is a quick run-down of the hierarchy and code: class Base { virtual void OneOfManyVirtuals() = 0; // Serialization Interface template<class Archive> void serialize(Archive &ar, const unsigned int version) { //empty } }; BOOST_IS_ABSTRACT(Base); class Derived : virtual public Base { virtual void OneOfManyVirtuals() { } // Serialization Interface template<class Archive> void serialize(Archive &ar, const unsigned int version) { //empty ar & BOOST_SERIALIZATION_BASE_OBJECT_NVP(Base); } } // later in the main code Base* pBase = new Derived; boost::archive::xml_oarchive oa(ofs); // doesn't work oa << BOOST_SERIALIZATION_NVP(pBase); // works oa << BOOST_SERIALIZATION_NVP(*pBase); When if fails, it fails in the oseralizer.hpp at line 419 with the comment: // convert pointer to more derived type. if this is thrown // it means that the base/derived relationship hasn't be registered I tried registering the pairs with void_cast_register, but I wasn't successful. Any real examples of how to use this method would be appreciated. So, does anyone know why the pointer to the base doesn't work when using virtual inheritance? It works find when I remove the virtual from the inheritance. Unfortunately, with the software architecture that I have, there are several cases when the virtual inheritance off of interfaces is required. Any help with the matter is greatly appreciated. -Eric
This should should work but it requires that derived types be either "registered" or "exported". This is eplained in the manual under serialization of pointers and under serialization traits. Basically you need either: class Base { virtual void OneOfManyVirtuals() = 0; // Serialization Interface template<class Archive> void serialize(Archive &ar, const unsigned int version) { //empty ar.register<Derived>(); // for each derived class } Or for each derived class (in its own header): BOOST_CLASS_EXPORT(Derived) That will solve the problem. Robert Ramey Eric wrote:
Boost version: 1.32.0
I am trying to use the serialization class to serialize data pointed to by the virtual base class. Note that the code works great if I don't use virtual inheritance off of the base class. Here is a quick run-down of the hierarchy and code:
class Base { virtual void OneOfManyVirtuals() = 0; // Serialization Interface template<class Archive> void serialize(Archive &ar, const unsigned int version) { //empty } };
BOOST_IS_ABSTRACT(Base);
class Derived : virtual public Base { virtual void OneOfManyVirtuals() { } // Serialization Interface template<class Archive> void serialize(Archive &ar, const unsigned int version) { //empty ar & BOOST_SERIALIZATION_BASE_OBJECT_NVP(Base); } }
// later in the main code Base* pBase = new Derived; boost::archive::xml_oarchive oa(ofs);
// doesn't work oa << BOOST_SERIALIZATION_NVP(pBase);
// works oa << BOOST_SERIALIZATION_NVP(*pBase);
When if fails, it fails in the oseralizer.hpp at line 419 with the comment: // convert pointer to more derived type. if this is thrown // it means that the base/derived relationship hasn't be registered
I tried registering the pairs with void_cast_register, but I wasn't successful. Any real examples of how to use this method would be appreciated.
So, does anyone know why the pointer to the base doesn't work when using virtual inheritance? It works find when I remove the virtual from the inheritance. Unfortunately, with the software architecture that I have, there are several cases when the virtual inheritance off of interfaces is required.
Any help with the matter is greatly appreciated.
-Eric
participants (2)
-
Eric
-
Robert Ramey