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