开发者

calling function multiple times with different arguments

开发者 https://www.devze.com 2023-02-10 21:37 出处:网络
I have a multiple arguments of type string m_strVar1 = \"var1\"; string m_strVar2 = \"var2\"; string m_strVar3 = \"var3\";

I have a multiple arguments of type

string m_strVar1 = "var1";
string m_strVar2 = "var2";
string m_strVar3 = "var3";
// i have like above like 10 variables.


for(int i = 1; i < 10 ; i++) {
string strArg = "m_strArg";
std::stringstream lStream;
lStream << argCnt;
strArg.append(lStream.str());
cout << "first argument is " << strArg.c_str() << endl;

// call the function
func1(strArg.c_str());
}


///////////////

void func1(string& arg1) {
    // here i am expecting to access m_strVar1/2 value, depending on argument but v开发者_如何学运维alue is not shown.
}

I think i am doing some thing wrong. can u please correct me. How can we achieve for calling the function with out calling 10 times with differnt values.


If I understand your code correctly, you're passing the name of the variable as a string, or something like that.

Instead use an array of strings, eg

string m_strVar[3] = { "var1", "var2", "var3" };
for (int i = 0; i < 3; i++)
    funct(m_strVar[i]);
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号