【问题标题】:how to retrieve true_type from an archive?如何从档案中检索 true_type?
【发布时间】:2012-10-29 21:55:40
【问题描述】:

上下文

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

我的问题

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

详情

让我们拥有这个类树:

ISerializable

如果我这样做:

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 的存档中。我的问题又来了:如何,仅使用存档对象(从存档文件构造而没有错误),我可以检索此密钥吗?

【问题讨论】:

    标签: c++ serialization boost polymorphism


    【解决方案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,所以loadad的类型也是D。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-12-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多