【发布时间】:2015-08-27 10:11:10
【问题描述】:
我正在尝试使用 Cereal 1.1.2 序列化和反序列化多态类(具有虚拟继承)。我收到“访问冲突 - 没有 RTTI 数据!”当我在反序列化后尝试将其向下转换为派生类时出现异常。当我使用普通继承而不是虚拟继承时,它工作正常。我已经在 Visual Studio 2013 社区版的项目设置中启用了 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;
}
【问题讨论】:
-
对我来说,您的示例只是段错误,因为它无法将反序列化的智能指针转换为子类...
-
快速评论 - 在对结果进行任何操作之前,您应该始终确保档案已被销毁。当它们超出范围时,它们将被销毁。有关详细信息,请参阅documentation。
标签: c++ inheritance serialization cereal