Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7164293
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T14:03:07+00:00 2026-05-28T14:03:07+00:00

I need a table that maps codes to C++ member functions. Suppose we have

  • 0

I need a table that maps codes to C++ member functions. Suppose we have this class:

class foo
{
  bool one() const;
  bool two() const;
  bool call(char*) const;
};

What I want is a table like this:

{
  { “somestring”,  one },
  { ”otherstring”, two }
};

So that if I have a foo object f, f.call(”somestring”) would look up “somestring” in the table, call the one() member function, and return the result.

All of the called functions have identical prototypes, i.e., they are const, take no parameters, and return bool.

Is this possible? How?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-28T14:03:07+00:00Added an answer on May 28, 2026 at 2:03 pm

    Since you only need to store members of the same class, with the same arguments and return types, you can use pointer-to-member-functions:

    bool foo::call(char const * name) const {
        static std::map<std::string, bool (foo::*)() const> table 
        {
            {"one", &foo::one}, 
            {"two", &foo::two}
        };
    
        auto entry = table.find(name);
        if (entry != table.end()) {
            return (this->*(entry->second))();
        } else {
            return false;
        }
    }
    

    That uses the new initialisation syntax of C++11. If your compiler doesn’t support it, there are various other options. You could initialise the map with a static function:

    typedef std::map<std::string, bool (foo::*)() const> table_type;
    static table_type table = make_table();
    
    static table_type make_table() {
        table_type table;
        table["one"] = &foo::one;
        table["two"] = &foo::two;
        return table;
    }
    

    or you could use Boost.Assignment:

    static std::map<std::string, bool (foo::*)() const> table = 
        boost::assign::map_list_of
            ("one", &foo::one)
            ("two", &foo::two);
    

    or you could use an array, and find the entry with std::find_if (or a simple for loop if your library doesn’t have that yet), or std::binary_search if you make sure the array is sorted.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have an entity that maps to an external oracle table which is one
I have a need to create a table that maps two table values. There
So I have a class with three fields that maps to a table using
I need a table that stores key-value pairs, so I created one with a
I have an SQLite table that contains a BLOB I need to do a
I have a large database table that I need to display on a Windows
I have a table that contains maybe 10k to 100k rows and I need
I have a table that got into the db_owner schema, and I need it
I have a table that maps a user's permissions to a given object. So,
So I have a class that is a generic and it may need to,

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.