K
KK
Hello,
Please check the below mentioned class. I am looking for a smarter way
to define the function 'getValueByLabelMatch'. getValueByLabelMatch
takes a variable label and retrieves the value of the respective class
member. For simplicity I have used members of int type, but they could
be of any data type.
/* variable scope deliberately given public scope */
#include <iostream>
using namespace std;
class someclass
{
public:
int nApples, nOranges, nGrapes;
someclass(int a,int b, int c):nApples(a),nOranges(b),nGrapes(c){}
int getValueByLabelMatch(string givestr)
{
if (givestr.compare("nApples")==0 )
return nApples;
//other if conditions for each of the member variables
return 0;
}
};
void main()
{
someclass tst(1,2,3);
int a = tst.getValueByLabelMatch("nApples");
}
Any ideas? Please advice.
Thank you.
Please check the below mentioned class. I am looking for a smarter way
to define the function 'getValueByLabelMatch'. getValueByLabelMatch
takes a variable label and retrieves the value of the respective class
member. For simplicity I have used members of int type, but they could
be of any data type.
/* variable scope deliberately given public scope */
#include <iostream>
using namespace std;
class someclass
{
public:
int nApples, nOranges, nGrapes;
someclass(int a,int b, int c):nApples(a),nOranges(b),nGrapes(c){}
int getValueByLabelMatch(string givestr)
{
if (givestr.compare("nApples")==0 )
return nApples;
//other if conditions for each of the member variables
return 0;
}
};
void main()
{
someclass tst(1,2,3);
int a = tst.getValueByLabelMatch("nApples");
}
Any ideas? Please advice.
Thank you.