簡體   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