简体   繁体   English

dynamic_cast到派生类型失败,为什么?

[英]dynamic_cast to derived type fails, Why?

I'd like to have some help on an issue I face. 我想在遇到的问题上有所帮助。

I'm using serialization (TinyXml to be precise) to save objects in a file and I have a problem on deserialization. 我正在使用序列化(准确地说是TinyXml)将对象保存在文件中,而在反序列化方面遇到问题。

My objects contain pointers, which are not saved directly but instead the ID of the object pointed to is saved. 我的对象包含不直接保存的指针,而是保存了指向的对象的ID。

Every object I save derivate ASerializable, directly or not. 我保存的每个对象都可以直接导出,也可以不直接导出。

Here is my hierarchy : 这是我的层次结构:

//Holds pure virtual method: TiXmlElement * ToXml() = 0;
class ISerializable;

// Basic implementation of ISerializable, this class has a unique ID.
class ASerializable : public ISerializable;

// For security sake, this class is not real, just a demonstration of my problem.
// Overrides TiXmlElement * ToXml()
class Base : public ASerializable;

// Overrides TiXmlElement * ToXml()
class Derived : public Base;

Upon deserialization, I add each object I create into a static map(ID, ASerializable*), which holds the data until dereference. 反序列化后,我将创建的每个对象添加到静态映射(ID,ASerializable *)中,该静态映射将保存数据直到取消引用为止。

How I dereference objects 我如何取消引用对象

I use a template class which holds a T**, which is the pointer to dereference once all the deserialization is done. 我使用的模板类包含一个T **,这是所有反序列化完成后要取消引用的指针。

template<typename T>
class Reference<T> {
    T** ref_ptr;
    int id; // This is the ID of the referenced object
    void Dereference();
}

Basically, Reference<T>::Dereference() gets the object in the map, then cast it to the good type (which is T* ) and change the value of *ref_ptr to the cast object. 基本上, Reference<T>::Dereference()获取映射中的对象,然后将其*ref_ptr转换为良好类型(即T* ),并将*ref_ptr的值更改为*ref_ptr转换对象。

I'm using dynamic_cast<T*>( objects_map[id] ) to cast my object and for some reason, it fails everytime. 我正在使用dynamic_cast<T*>( objects_map[id] )投射对象,由于某种原因,它每次都会失败。

I can't cast from a ASerialisable* to a Derived* without losing the data of Derived . 我不能从ASerialisable*转换为Derived*而不丢失Derived的数据。

Here is the pattern I want to achieve : 这是我想要实现的模式:

  1. On deserialized, the item add itself to the objects map; 反序列化后,该项目将自身添加到对象图;

  2. If the object is a reference, add a Reference (with the good type, I know it) to a Stack 如果对象是引用,则将引用(具有好的类型,我知道)添加到堆栈中

  3. Once all deserialization is done, unstack all references and calls Dereference() 完成所有反序列化后,请取消堆叠所有引用并调用Dereference()

  4. Be really happy. 真的要开心

Thank you soooo dor your time and precious help, really appreciated. 非常感谢您的宝贵时间和宝贵的帮助,非常感谢。

{enjoy} EDIT : Okay, I'll try to be clearer, but it is a bit complex. {enjoy}编辑:好的,我会尝试更清楚一些,但这有点复杂。

When I parse my XML file back, there are sometimes element called <reference id="X"> : those are reference to another instance ( ASerializable ); 当我解析我的XML文件时,有时会有一个称为<reference id="X">元素:这些是对另一个实例的引用( ASerializable )。

When I encounter a <reference> object, I add it to a stack, to be dereferenced later. 当遇到<reference>对象时,我将其添加到堆栈中,以供以后取消引用。 When I encounter a specific object, like a <Base id="X" ... data ... /> I build it, then I add it to a object map, as an ASerializable* ; 当我遇到一个特定的对象(例如<Base id="X" ... data ... />时,我将其构建,然后将其作为ASerializable*添加到对象映射中;

Once I finished building all my objects, I unstack each of the reference and call Dereference() , that SHOULD go get the REAL INSTANCE of the object, cast it to the good type using dynamic_cast then change the value of his ref_ptr to point that object. 一旦我完成所有对象的构建,就拆开每个引用并调用Dereference() ,应该去获取对象的真实实例,使用dynamic_cast将其转换为良好的类型,然后更改其ref_ptr的值以指向该对象。

Don't hesitate to ask for other questions if needed. 如有需要,请随时询问其他问题。

Does your base class have at least one virtual method? 您的基类是否至少有一个虚拟方法? If it has none, make the destructor virtual. 如果没有,则将析构函数设为虚拟。


You might want to look into using Boost.Serialization , it handles serializing to and from XML files nicely. 您可能想研究使用Boost.Serialization ,它可以很好地处理与XML文件之间的序列化。

Alright, thank you all for your time. 好,谢谢大家的时间。 I found the problem. 我发现了问题。

Everything was fine in my hierarchy, the problem was a vector that was being copied. 在我的层次结构中,一切都很好,问题是正在复制的向量。 The pointers it contained were then changing so my the ref_ptr of Reference was corrupted. 然后它包含的指针发生了变化,因此我的Reference的ref_ptr损坏了。

{enjoy} {请享用}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM