指向复合类数据成员的指针

编程入门 行业动态 更新时间:2024-10-28 00:23:16
本文介绍了指向复合类数据成员的指针 - 第2部分的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

除了我之前的问题, C ++:指向复合类数据成员:

对不起,我没有描述我的需要。在我看来有点复杂,解释。但是,根据要求,请在下面找到我的问题的简要说明。

Sorry for not having described my needs. It seems to me a bit complex to explain. But, as asked, please find below a brief description of my problem.

我想创建一个从XML自动填充的参数类。

I would like to create a parameter class that is automatically filled from an XML.

为此,我将该参数类的每个数据成员添加到指向与其XML标记名称相关联的成员的指针的向量中。

To do that, I add each data member of that parameter class into a vector of pointers to members associated with their XML tag names.

在XML读取期间,从XML中读取所有标记名称,并更新所有参数的值。

During the XML reading all the tag names are read from the XML and all the parameter's values are updated.

我没有找到任何方法来声明一个成员指针指向我类的stParam1.fVal1成员(见下面注释的行)。

I haven't find any way to declare a member pointer to the "stParam1.fVal1" member of my class (see the line commented below).

如何声明一个成员指针类的结构?

class XmlReader { public : struct ST_PARAM { float XmlReader::*ptrFloat; string tagName; }; void addAttribut(float XmlReader::* pfMembre, string& tagName) { ST_PARAM stParam; stParam.ptrFloat = pfMembre; stParam.tagName = tagName; _tstParams.push_back(stParam); } void ReadParameters(string& fileName){/*...*/} // Read Xml File and update all parameters in _tstParams private: vector<ST_PARAM> _tstParams; }; class Param : public XmlReader { public: Param() { addAttribut((float XmlReader::*)&Param::fVal1, string("XML_TAG_NAME_1")); // OK addAttribut((float XmlReader::*)&Param::fVal2, string("XML_TAG_NAME_2")); // OK // addAttribut((float XmlReader::*)&Param::stParam1.fVal1, string("XML_TAG_NAME_3")); // DON'T WORK -> Syntax is invalid //... } // Some more complex parameters types typedef struct { float fVal1; float fVal2; }ST_PARAM_1; //... // Declaration of several parameters ST_PARAM_1 stParam1; F32 fVal1; F32 fVal2; //... }; void test() { Param myParam; myParam.ReadParameters(string("Parameters.xml")); }

推荐答案

XmlReader :: 和 Param :: 的内容?

我认为你正在围绕你的数据成员建立围栏,只是为了跳过他们的快乐...

I think you're building fences around your data members just for the pleasure of jumping over them...

保持简单...

class XmlReader { public : struct ST_PARAM { float *ptrFloat; string tagName; }; void addAttribut(float* pfMembre, string tagName) { ST_PARAM stParam; stParam.ptrFloat = pfMembre; stParam.tagName = tagName; _tstParams.push_back(stParam); } void ReadParameters(string fileName){/*...*/} // Read Xml File and update all parameters in _tstParams private: vector<ST_PARAM> _tstParams; }; class Param : public XmlReader { public: Param() { addAttribut(&fVal1, "XML_TAG_NAME_1"); // OK addAttribut(&fVal2, "XML_TAG_NAME_2"); // OK addAttribut(&stParam1.fVal1, "XML_TAG_NAME_3"); // OK... //... }

更多推荐

指向复合类数据成员的指针

本文发布于:2023-07-28 02:47:12,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1226590.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:指针   成员   数据

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!