序列化使用boost序列化类,而不会改变类 [英] serializing classes using boost serialization without changing the class
问题描述
这片code有我们每一个时间来写一个类iefrom 模板<等级档案>
到 AR
&安培; BOOST_SERIALIZATION_NVP(B)
。我们怎样才能使它短?
以及我们如何可以序列化STL容器?
类Employee
{
私人的:
友元类的boost ::系列化::访问;
模板<类归档和GT;无效连载(归档和放大器; AR,
const的无符号整型版)
{
AR&安培; BOOST_SERIALIZATION_NVP(一);
AR&安培; BOOST_SERIALIZATION_NVP(二);
} int类型的;
INT B:上市:
员工(INT A,INT B)
{
这 - >一种=一个;
这 - > B =;
}};
我建议你先从文档:)
STL容器是可序列化时,包括相关的标题:
的#include<升压/系列化/ map.hpp>
#包括LT&;升压/系列化/ string.hpp>
其他的东西主机支持开箱即用。
您的可能的制作一般包装为已取得reflectible通过其他方式类型(例如融合顺序,Qt的QObject对象等)
this piece of code has to be written every time we make a class i.e.from template<class archive>
to ar
& BOOST_SERIALIZATION_NVP(b)
. How can we make it short?
and how can we serialize the stl containers ?
class Employee
{
private:
friend class boost::serialization::access;
template<class Archive> void serialize(Archive & ar,
const unsigned int version)
{
ar & BOOST_SERIALIZATION_NVP(a);
ar & BOOST_SERIALIZATION_NVP(b);
}
int a;
int b;
public:
Employee(int a, int b)
{
this->a = a;
this->b = b;
}
};
I suggest you start with the documentation :)
- http://www.boost.org/doc/libs/1_55_0/libs/serialization/doc/index.html (start under Serializable Concept)
STL containers are serializable when you include the relevant header:
#include <boost/serialization/map.hpp>
#include <boost/serialization/string.hpp>
A host of other stuff is supported out of the box.
You could make generic wrappers for types that have already been made "reflectible" by other means (e.g. Fusion Sequences, Qt QObjects etc.)
这篇关于序列化使用boost序列化类,而不会改变类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!