如何保存模板向量的类型信息

发布于 2024-10-03 23:15:30 字数 1045 浏览 5 评论 0原文

我正在尝试实现一个序列化类(现在是 XML)。这个想法是任何派生类都可以向基类注册其成员,并且基类可以以 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 是不同的类型)

一种可能的方法可能是

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

那伤。 2024-10-10 23:15:30

FWIW这个答案 (by @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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文