Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Access class members by its string label

2 views
Skip to first unread message

KK

unread,
Jun 29, 2009, 7:35:24 PM6/29/09
to
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.

Alf P. Steinbach

unread,
Jun 29, 2009, 7:37:19 PM6/29/09
to
* 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

--
Due to hosting requirements I need visits to <url: http://alfps.izfree.com/>.
No ads, and there is some C++ stuff! :-) Just going there is good. Linking
to it is even better! Thanks in advance!

KK

unread,
Jun 29, 2009, 8:48:30 PM6/29/09
to

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.

Sam

unread,
Jun 29, 2009, 9:32:28 PM6/29/09
to
KK writes:

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

Then whoever designed such a class needs to be commited to the looney bin,
and not be allowed anywhere near a C++ compiler.

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

Not in C++. If you want that kind of functionality, use Java. There is no
C++ equivalent of java.lang.reflect.


Chris M. Thomasson

unread,
Jun 29, 2009, 11:53:16 PM6/29/09
to
"KK" <peda...@gmail.com> wrote in message
news:96db5677-826b-4bfa...@h11g2000yqb.googlegroups.com...

On Jun 29, 4:37 pm, "Alf P. Steinbach" <al...@start.no> wrote:
[...]

> 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?

James Kanze

unread,
Jun 30, 2009, 4:20:57 AM6/30/09
to
On Jun 30, 5:53 am, "Chris M. Thomasson" <n...@spam.invalid> wrote:
> "KK" <pedag...@gmail.com> wrote in message

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

--
James Kanze (GABI Software) email:james...@gmail.com
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34

Pascal J. Bourguignon

unread,
Jun 30, 2009, 4:36:03 AM6/30/09
to
KK <peda...@gmail.com> writes:

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

--
__Pascal Bourguignon__

Thomas Matthews

unread,
Jul 1, 2009, 2:10:56 AM7/1/09
to
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

KK

unread,
Jul 1, 2009, 2:29:06 PM7/1/09
to
On Jun 30, 1:36 am, p...@informatimago.com (Pascal J. Bourguignon)
wrote:

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.

Hendrik Schober

unread,
Jul 3, 2009, 1:34:15 PM7/3/09
to

Don't you think that there might be different solutions
depending on whether we're talking 10 members (fine) or
100 members (far off the scale) or 1000 members (simply
ridiculous)?

Schobi

Pascal J. Bourguignon

unread,
Jul 6, 2009, 6:00:52 AM7/6/09
to
KK <peda...@gmail.com> writes:


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();
}

--
__Pascal Bourguignon__

0 new messages