指向复合类数据成员的指针 - 第2部分 [英] Pointer to composite class data member - Part 2

查看:93
本文介绍了指向复合类数据成员的指针 - 第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...
    //...
  }

这篇关于指向复合类数据成员的指针 - 第2部分的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆