access members by string name

Discussion in 'C++' started by Gernot Frisch, Aug 30, 2011.

  1. 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] () ; }
    };
    Gernot Frisch, Aug 30, 2011
    #1
    1. Advertising


  2. > 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.
    Gernot Frisch, Aug 31, 2011
    #2
    1. Advertising

  3. On 30 août, 16:53, "Gernot Frisch" <> 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>::iterator 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
    Michael DOUBEZ, Aug 31, 2011
    #3
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. JFCM
    Replies:
    4
    Views:
    5,723
  2. CoolPint
    Replies:
    8
    Views:
    957
    Jeff Schwab
    Dec 14, 2003
  3. Dave
    Replies:
    3
    Views:
    359
    tom_usenet
    Aug 10, 2004
  4. hdixon
    Replies:
    3
    Views:
    627
    hdixon
    Jul 9, 2006
  5. lovecreatesbeauty
    Replies:
    43
    Views:
    1,287
    Keith Thompson
    Feb 6, 2006
Loading...

Share This Page