我有一个“生成器”类,基本上构造了它的子类。为了使用这个东西,我简单地将它分类并传递正确的参数来构建我想要构建的对象。我想序列化这些东西,因为所有的数据都在基础中,所以没有很好的理由针对每个子类。以下是我已经有了为例:boost.serialization - 免费版本和基类实现
#include <boost/serialization/serialization.hpp>
template < typename T >
struct test_base
{
// works...
//template < typename Archive >
//void serialize(Archive &, unsigned int const)
// {
//}
};
template < typename T >
void f(test_base<T> const&) {}
struct test_derived : test_base<int>
{
};
namespace boost { namespace serialization {
template < typename Archive, typename T >
void serialize(Archive &, test_base<T> &, unsigned int const)
{
}
}}
#include <boost/archive/binary_oarchive.hpp>
#include <sstream>
int main()
{
int x = 5;
test_derived d;
//boost::serialization::serialize(x, d, 54); // <- works.
std::ostringstream str;
boost::archive::binary_oarchive out(str);
out & d; // no worky.
}
我想免费的版本,如果可能的工作。是吗?
上面的版本吐出错误关于serialize不是test_derived的成员。