c++ - 使用虚拟继承对多态类使用 Cereal 进行序列化

标签 c++ inheritance serialization cereal

我正在尝试使用 Cereal 1.1.2 序列化和反序列化多态类(具有虚拟继承)。我收到“访问违规 - 没有 RTTI 数据!”当我在反序列化后尝试将其向下转换为派生类时出现异常。当我使用普通继承而不是虚拟继承时它工作正常。我已经在 Visual Studio 2013 Community Edition 的项目设置中启用了 RTTI (/GR)。这是我的代码:

class Boogie
{
    friend class cereal::access;
    virtual void virtualFunction() {}
    int boogieInt = 3;
    template<class Archive>
    void serialize(Archive & archive)
    {
        archive(boogieInt);
    }
};

class Booga : virtual public Boogie
{
    friend class cereal::access;
public:
    void virtualFunction() {}
    int boogaInt = 2;
    template<class Archive>
    void serialize(Archive & archive)
    {
        archive(cereal::virtual_base_class<Boogie>(this), boogaInt);
    }
};

CEREAL_REGISTER_TYPE(Booga);

int _tmain(int argc, _TCHAR* argv[])
{
    try
    {
        {
            std::shared_ptr<Boogie> boogie = std::make_shared<Booga>();
            std::ofstream ofs("Booga.txt");
            cereal::BinaryOutputArchive archive(ofs);
            archive(boogie);
            ofs.close();
        }

        std::shared_ptr<Boogie> deBoogie;
        std::ifstream ifs("Booga.txt");
        cereal::BinaryInputArchive iarchive(ifs);
        iarchive(deBoogie);

        std::shared_ptr<Booga> outBooga = std::dynamic_pointer_cast<Booga>(deBoogie);

        std::cout << outBooga->boogaInt << std::endl;

        std::cin.get();
    }
    catch (std::exception e)
    {
        std::cout << "EXCEPTION" << std::endl;
        std::cout << e.what() << std::endl;
    }
    return 0;
}

最佳答案

你的问题是你正在保存和加载不同的类型 - 你的加载代码应该反射(reflect)你的保存代码。

另请注意,您无需在此处使用虚拟继承,因为您仅派生自单个父类 - 参见 here了解更多信息)。此外,请参阅我对您关于如何以 RAII 方式正确使用文件的帖子的评论。

您的输出可以指定为:

std::shared_ptr<Boogie> data = std::make_shared<Booga>();
archive( data ); // data is actually a Booga but we hold it in a Boogie ptr

请注意,我分配了一个 Boogie 对象,尽管我分配了一个 Booga 指针 - 这基本上是多态性的全部要点,除非你需要这样做,否则不要'不使用多态性。

现在当我们加载时,我们加载到我们序列化的相同类型:

std::shared_ptr<Boogie> data;
archive( data ); // data is actually a Booga object because of polymorphism

只需确保您实际传递给存档的变量类型是相同的,无论它们由于多态性而实际是什么。

关于c++ - 使用虚拟继承对多态类使用 Cereal 进行序列化,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32246562/

相关文章:

c++ - 虚函数覆盖是否违反LSP?

c++ - 在 Linux 上锁定单个进程内数据访问的最快方法

Java 套接字 : Request-response pattern with object streams

C#反序列化问题

Android 应用程序在方向更改时重置,最佳处理方式?

c++ - 尝试在 Visual Studio 中链接 libclang 时出现链接错误

c++ - 获取 istringstream 的未读部分

c++ - 使用基对象修改/访问派生类信息的问题设计

C++:字段的类型不完整

ruby-on-rails - 通过继承检索的记录上调用ActiveRecord::Relation的未定义方法 `update_attribute'