Access class members by its string label

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.
 
A

Alf P. Steinbach

* 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;

Don't make member variables public except in pure data classes (POD structures).

someclass(int a,int b, int c):nApples(a),nOranges(b),nGrapes(c){}
int getValueByLabelMatch(string givestr)

Preferentially pass a std::string as 'std::string const& s'.

{
if (givestr.compare("nApples")==0 )

Just use '==", that is, the condition 'givestr == "nApples"'.

return nApples;
//other if conditions for each of the member variables
return 0;
}
};
void main()

'main' must always have result type 'int'.

{
someclass tst(1,2,3);
int a = tst.getValueByLabelMatch("nApples");
}

Any ideas? Please advice.

std:map


Cheers & hth.,

- Alf
 
K

KK

* KK:







Don't make member variables public except in pure data classes (POD structures).


Preferentially pass a std::string as 'std::string const& s'.


Just use '==", that is, the condition 'givestr == "nApples"'.


'main' must always have result type 'int'.



std:map

Cheers & hth.,

- Alf

I missed my point a bit. What if the class has 1000 variables instead
of just 3? Is there anyway I could skip the process of creating a map
for each and every variable and instead use an iterator to loop
through all the members until I find a match.
 
C

Chris M. Thomasson

I missed my point a bit. What if the class has 1000 variables instead
of just 3? Is there anyway I could skip the process of creating a map
for each and every variable and instead use an iterator to loop
through all the members until I find a match.

Don't you think that using a (hash) map might be faster than iterating
through each and every variable?
 
J

James Kanze

"KK" <[email protected]> wrote in message
[...]
I missed my point a bit. What if the class has 1000
variables instead of just 3? Is there anyway I could skip
the process of creating a map for each and every variable
and instead use an iterator to loop through all the members
until I find a match.
Don't you think that using a (hash) map might be faster than
iterating through each and every variable?

It's also easier to use. And std::map can be initialized from a
static table, so it's easy to maintain as well (although since
std::map requires dynamic initialization, so there could be an
order of initialization issue).
 
P

Pascal J. Bourguignon

KK said:
I missed my point a bit. What if the class has 1000 variables instead
of just 3?

Then definitely use a map.

Is there anyway I could skip the process of creating a map
for each and every variable and instead use an iterator to loop
through all the members until I find a match.

What do you think java does to match the name of the slot to the slot itself?
Yes, a map.

Now if you are complaining that C++ is lower level than Java, there
you get what you asked for.
 
T

Thomas Matthews

KK wrote:
[snip]
I missed my point a bit. What if the class has 1000 variables instead
of just 3? Is there anyway I could skip the process of creating a map
for each and every variable and instead use an iterator to loop
through all the members until I find a match.

Do you actually have a class with 1000 variables?
I've never seen one.
Are you an extremist?

In writing compilers, USB interface parsers and other entities,
I have never had a class with over 1000 variables.

There is another exception and that may be configuration items.
Most of these are of the type <key, value> pairs.
I still haven't put all of them in one class, I group by theme.

--
Thomas Matthews

C++ newsgroup welcome message:
http://www.slack.net/~shiva/welcome.txt
C++ Faq: http://www.parashift.com/c++-faq-lite
C Faq: http://www.eskimo.com/~scs/c-faq/top.html
alt.comp.lang.learn.c-c++ faq:
http://www.comeaucomputing.com/learn/faq/
Other sites:
http://www.josuttis.com -- C++ STL Library book
http://www.sgi.com/tech/stl -- Standard Template Library
 
K

KK

Then definitely use a map.


What do you think java does to match the name of the slot to the slot itself?
Yes, a map.

Now if you are complaining that C++ is lower level than Java, there
you get what you asked for.

That was insightful. Is there a sample code which exemplifies this
idea?

No I dont actually use 1000 variables in my code. I just meant to push
the problem to make one think out of the box.
 
P

Pascal J. Bourguignon

KK said:
That was insightful. Is there a sample code which exemplifies this
idea?

No I dont actually use 1000 variables in my code. I just meant to push
the problem to make one think out of the box.


class WithALotoSlots {
private:
typedef std::map<std::string,Object> SlotMap;
SlotMap slots;
public:
void setSlot(const std::string& slotName,Object& slotValue){
slots[slotname]=slotValue;
}
Object& getSlot(const std::string& slotName){
SlotMap::iterator it=slots.find(slotName);
if(it==slots.end()){
throw std::exception((std::string("Unknown slot named ")+slotName).c_str());
}else{
return(it->second);
}
}
}

void example(){
WithALotoSlots w;
w.setSlot("x")=Object(1.0);
w.getSlot("x").print();
}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,770
Messages
2,569,584
Members
45,075
Latest member
MakersCBDBloodSupport

Latest Threads

Top