我在尝试序列化模板类的消息时遇到问题。模板的类消息是BaseClass类型,但我希望它序列化类的派生版本。到目前为止,它只是序列化基类。我应该如何向boost::serialization注册我希望它序列化的派生类的类型?我尝试过几种方法,比如使用“BOOST_CLASS_EXPORT”宏。我也尝试过使用这个例子:
ar.register_type(static_cast<bus_stop_corner *>(NULL));
从这里开始:
http://www.boost.org/doc/libs/1_36_0/libs/serialization/example/demo_gps.hpp
但还是没有运气。
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;
}
这段代码根本不可编译,但包含它是为了让您了解我的程序的结构。