Re: access members by string name
"Gernot Frisch" <me@privacy.net> wrote in
news:9c4brfFfu2U1@mid.individual.net:
Hi,
assume you have a class with members (or member get/set functions) and
want to call them by a string name with the least code to maintain.
I like to just have one macro line per member. The class should have a
working assignment operator.
The end class should look like:
class foo
{
int bar;
int goo;
public:
...
int GetGoo()const {return goo;}
void SetGoo(int g) {goo = g;}
...
std::map<std::string, something_to_map_the_GET_function> getfunctions;
static std::string GetAllParams() {return std::string("bar,goo");}
int GetVar(std::string name) {return getfunctions[name] () ; }
};
What's wrong with
class foo {
std::map<std::string, int> data_;
public:
int GetVar(const std::string& name) const {
std::map<std::string, int>::const_iterator p = data_.find(name);
return p==data_.end()? 0: p->second;
}
int GetGoo()const {return GetVar("goo");}
void SetGoo(int g) {data_["goo"] = g;}
};
cheers
Paavo