问题描述
我正在尝试实现一个用于序列化的类(目前为XML).这个想法是,任何派生类都可以在基类中注册其成员,而基类可以以XML的形式编写成员.
I am trying to implement a class for serialization (XML for now). The idea is that any derived class can registers its members with the base class and base can write the members in form of XML.
代码看起来像这样
class IXMLINF
{
protected:
struct INFObj
{
union MemPtr
{
int* piMem;
char* pstrMem;
IXMLINF* pINFMem;
}
MemPtr memObj;
};
vec<INFObj*> m_INFObjVec;
void addMemToINF(int* piMem)
{
INFObj* pObj = new INFObj;
pObj->memObj.piMem = piMem;
m_INFObjVec.append(pObj);
}
void addMemToINF(char* pstrMem);
void addMemToINF(IXMLINF* pINFMem);
void writeToXML()
{
for_each_element_in_m_INFObjVec
{
//if int or char process to XML
//else if IXMINF call its writeToXML
}
}
}
到目前为止,一切都很好.但是,我也希望能够将类型的向量写入XML.对于int和char *,这很容易,但是如何以通用方式对IXMLINF派生类的向量进行处理(vec与vec的类型不同)
So far so good . However I also want to to be able to write vectors of types to XML. For int and char* it is easy but how to do it for vectors of IXMLINF derived class in a generic way (vec is a different type from vec)
一种可能的方式可能是
<class T>void addMemToINF(vec<T*>* pXMem)
{
//T is a class derived from IXMLINF
void* pvMem = (void*)pXMem
//Somehow store type of T
Type = T
}
void writeToXML()
{
....
vec<Type*>* pVec = (vec<Type*>*)pvMem ;
}
我将对有关如何存储类型信息(类型= T步骤)的任何建议或执行我想做的任何替代方法表示感谢.
I will appreciate any suggestions on how to store Type informatio (Type = T step) or any alternate method for doing what I want to do.
推荐答案
FWIW此(@Phillip)也通过一点点调整就回答了这个问题.如果有人愿意的话,我可以放罐子.
FWIW this answer (by @Phillip) to a related question also answers this question with a little bit of tweaking . If anybody wants I can put the soln.
这篇关于如何保存模板矢量的类型信息的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!