How to save type information of template vector

北城以北 提交于 2019-12-12 06:25:46

问题


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.

The code looks something like this

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
}
}
}

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)

One possible way might be

<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 ;

}

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.


回答1:


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.



来源:https://stackoverflow.com/questions/4273869/how-to-save-type-information-of-template-vector

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!