盲目地在函数中定义成员变量
Define member variables in functions blindly
我当前的任务有点问题。基本上,我得到了一个 XML 文件,并试图解析它以获取关键信息。例如,有些行会是这样的:
<IPAddress>123.45.67</IPAddress>
我要得到 123.45.67 的值,一点也不差。有人告诉我不要使用 XML 解析器,而是手动解析它,这很容易。但是,我对任务的第二部分有疑问。基本上,我要用某些成员变量创建一个 class 并根据我解析的值声明它们。所以假设 class 被称为 Something 并且有一个名为 IPAddress 的成员变量。然后我要将 IPAddress 的值更新为 123.45.67,因此当有人在 main 方法中调用 Something.IPAddress 时,它会 returns 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 语句会很烦人并且不受欢迎。有没有其他方法可以以某种方式从 class 访问成员变量?抱歉,如果我的问题很长,我只是想确保提供理解问题所需的一切!请让我知道是否应该添加任何可能不存在的重要内容。
非常感谢!
这可能被认为是不好的风格,但我通常只是这样做:
// 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;
我当前的任务有点问题。基本上,我得到了一个 XML 文件,并试图解析它以获取关键信息。例如,有些行会是这样的:
<IPAddress>123.45.67</IPAddress>
我要得到 123.45.67 的值,一点也不差。有人告诉我不要使用 XML 解析器,而是手动解析它,这很容易。但是,我对任务的第二部分有疑问。基本上,我要用某些成员变量创建一个 class 并根据我解析的值声明它们。所以假设 class 被称为 Something 并且有一个名为 IPAddress 的成员变量。然后我要将 IPAddress 的值更新为 123.45.67,因此当有人在 main 方法中调用 Something.IPAddress 时,它会 returns 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 语句会很烦人并且不受欢迎。有没有其他方法可以以某种方式从 class 访问成员变量?抱歉,如果我的问题很长,我只是想确保提供理解问题所需的一切!请让我知道是否应该添加任何可能不存在的重要内容。
非常感谢!
这可能被认为是不好的风格,但我通常只是这样做:
// 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;