Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > access members by string name

Reply
Thread Tools

access members by string name

 
 
Gernot Frisch
Guest
Posts: n/a
 
      08-30-2011
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] () ; }
};


 
Reply With Quote
 
 
 
 
Gernot Frisch
Guest
Posts: n/a
 
      08-31-2011


> class foo {
> std::map<std::string, int> data_;


It's a slight overhead, but it works properly for what I want. It's obvious,
but I was unable to see. Thanks for opening my eyes.

 
Reply With Quote
 
 
 
 
Michael DOUBEZ
Guest
Posts: n/a
 
      08-31-2011
On 30 août, 16:53, "Gernot Frisch" <(E-Mail Removed)> wrote:
> 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");}


// type for pointer to foo's member that is accessible.
typedef int (foo::*accessible_member_ptr);

static std::map<std::string,accessible_member_ptr> member_ptr;

static accessible_member_ptr
getfunctions(std::string name)
{
std::map<std::string,accessible_member_ptr>::itera tor it =
member_ptr.find(name);
if( name == member_ptr.end() ) throw ParamDoesNotExists(name);

return it->second;
}

int GetVar(std::string name)
{
return this->*getfunctions(name);
}
int SetVar(std::string name, int newValue)
{
this->*getfunctions(name) = newValue;
}

};

// somewhere
member_ptr["bar"] = &foo::bar;
member_ptr["goo"] = &foo::goo;

--
Michael
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
class members vs instance members hdixon Python 3 07-09-2006 06:56 PM
Difference between static final members and final static members(if any)? JFCM Java 4 02-07-2006 11:32 AM
Which members are created automatically, which members are not inherited? lovecreatesbeauty C Programming 43 02-06-2006 11:36 PM
Templates: Members Vs. non-members Dave C++ 3 08-10-2004 11:23 AM
Can nested class members access private members of nesting class? CoolPint C++ 8 12-14-2003 02:30 PM



Advertisments