Boost logo

Boost Users :

Subject: [Boost-users] [Serialization] How do I serialize derived classes via a template class of type base class?
From: Neil Monday (nmonday_at_[hidden])
Date: 2010-07-07 16:49:18


I am having a problem where I am trying to serialize a message of a
template class. The template's class message is of type BaseClass, but I
want it to serialize the derived versions of the class. As of right now,
it is only serializing the BaseClass. How am I supposed to register with
boost::serialization the types of the derived classes that I want it to
serialize? I have tried several methods such as using the
'BOOST_CLASS_EXPORT' macro. I have also tried
boost::serialization::void_cast_register(). Also, I have tried using
this example:

ar.register_type(static_cast<bus_stop_corner *>(NULL));

from here:
http://www.boost.org/doc/libs/1_36_0/libs/serialization/example/demo_gps.hpp
but still no luck.

It seems to me that all of the things I have posted above are different
ways to achieve the same end result: serialize a derived class via a
base class pointer. But I am not clear on when to use which one, and
which method is appropriate for my case. Here is an example of what my
code is like:

template <class T>
class Frame{
...
private:
     T message;
};

class BaseType{};

class SubTypeA : public BaseType{};

class SubTypeB : public BaseType{};

int main(){
     std::vector< Frame<BaseType> > myFrames;
     //add a bunch of Frame<SubTypeA> and Frame<SubTypeB> objects to the
myFrames vector.

     //serialize the vector.

     return 0;
}

This code is not compilable at all, but was included to give you an idea
of the structure of my program. I am pretty new to mailing lists, so I
apologize if I am not using proper formatting etc.


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