繁体   English   中英

盲目地在函数中定义成员变量

[英]Define member variables in functions blindly

我目前的工作有点问题。 基本上,我得到了一个XML文件,并试图对其进行解析以获取关键信息。 例如,某些行将如下所示:

<IPAddress>123.45.67</IPAddress>

我要得到的值是123.45.67,一点也不差。 有人告诉我不要使用XML解析器,而只是手动解析它,这非常容易。 但是,我对任务的第二部分有疑问。 基本上,我将使用某些成员变量来创建一个类,并根据我解析的值对其进行声明。 因此,假设该类称为Something,并且有一个名为IPAddress的成员变量。 然后,我将IPAddress的值更新为123.45.67,以便当有人在main方法中调用Something.IPAddress时,它返回123.45.67。 这是我最初的尝试:

#include <iostream>
#include <fstream>
#include <string>
#include <sys/stat.h>

using namespace std;

class Something
{
   public:
    string location;
    string IPAddress;
    string theName;
    int aValue;

    //loop through the array from the method below
    void fillContent(string* array)
    {
        for(int i = 0; i < array->size(); i++)
        {
              string line = array[i];
              if((line.find("<") != std::string::npos) && (line.find(">")!= std::string::npos)) 
              {
                 unsigned first = line.find("<");
                 unsigned last = line.find(">");
                 string strNew = line.substr (first + 1, last - first - 1); //this line will get the key, in this case, "IPAddress"
             unsigned newfirst = line.find(">");
                 unsigned newlast = line.find_last_of("<");
             string strNew2 = line.substr(newfirst + 1, newlast - newfirst - 1); //this line will get the value, in this case, "123.45.67"
                if(strNew == "IPAddress")
                {
                    IPAddress = strNew2; //set the member variable to the IP Address
                }
              }
        }
    }

    //this method will create an array where each element is a line from the xml
        void fillVariables()
    {
        string line;
        ifstream myfile ("content.xml");
        long num = //function that gets size that I didn't add to make code shorter!;
        string *myArray;
        myArray = new string[num];
        string str1 = "";
        string strNew2 = "";
        int counter = 0;
        if (myfile.is_open())
        {
            while ( getline (myfile,line) )
            {
            myArray[counter] = line;
                counter++;
            }
            myfile.close();
        }
        fillContent(myArray);
    }

};


int main(int argc, char* argv[])
{
  Something local;
  local.fillVariables();
  cout << local.IPAddress<< endl; // should return "123.45.67"
  return 0;
}

现在,它可以执行我想要的操作,但是,您可以看到我需要if语句。 假设我至少有这些成员变量中的20个,那么拥有20个if语句将很烦人,只是皱眉。 我还有其他方法可以从类中访问成员变量吗? 抱歉,如果我的问题很长,我只是想确保提供理解问题所需的一切! 请让我知道是否应该添加一些可能不存在的重要内容。

非常感谢!

这可能被认为是不好的风格,但是我通常只是这样做:

// at the top of the 'fillContent' function
std::map<string, string*> varmap{
    {"IPAddress", &IPAddress},
    {"AnotherField", &AnotherField}
 };
 // If you're not using C++11, you can also try:
 // std::map<string, string*> varmap;
 // varmap["IPAddress"] = &IPAddress;
 // varmap["AnotherField"] = &AnotherField;

 // parsing code goes here
 *varmap[strNew] = strNew2;

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM