Pointer to composite class data member - Part 2

|▌冷眼眸甩不掉的悲伤 提交于 2019-12-08 14:17:45

问题


In addition to my previous question C++: Pointer to composite class data member :

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.

I would like to create a parameter class that is automatically filled from an 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.

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

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

How can I declare a member pointer to a struct of a class ?

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"));
}

回答1:


Why would you need all this XmlReader:: and Param:: stuff for ?

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

Keep It Simple...

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


来源:https://stackoverflow.com/questions/15881735/pointer-to-composite-class-data-member-part-2

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