3

语境

我使用 Boost 序列化库来保存和加载系统的对象。我围绕这个库定义了实践,所以我总是从一个基类序列化(每个可序列化的类都继承自ISerializable)。因此,true_type(即最派生的类型)与 this_type(即,ISerializable)不同,并且 true_type 存储在档案中。

我的问题

如何仅从存档对象中检索此 true_type(作为写入存档中的字符串)?

细节

让我们有这个类树:

ISerializable <|-- B <|-- D

如果我做:

B* b = new D();
b->SaveToFile(path); // <= this will do the serialization `ar & this`
                           (`this` being a `ISerializable*`)

我获得了一个存档,其中写入了 true_type “D”(无论存档的类型:​​txt、bin 或 xml)。

使用b对象和此代码:

const boost::serialization::extended_type_info & true_type
        = * boost::serialization::type_info_implementation<ISerializable>::type
            ::get_const_instance().get_derived_extended_type_info(*b);

我有我想要的true_type.get_key(),即:“D”。我可以验证在每个存档存储中都写入了“D” b。我的问题又来了:如何仅使用存档对象(从存档文件构造而没有错误)才能检索此密钥?

4

1 回答 1

1

它应该是这样的:

B * b;
ar >> b;//loading archive
const boost::serialization::extended_type_info & true_type
    = * boost::serialization::type_info_implementation<ISerializable>::type
        ::get_const_instance().get_derived_extended_type_info(*b);

因为保存的类型是 D,所以加载类型也是 D。

于 2013-09-18T09:59:49.663 回答