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 7735645
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T07:33:09+00:00 2026-06-01T07:33:09+00:00

My question is a little complicated so I’ll start with an example: class a

  • 0

My question is a little complicated so I’ll start with an example:

class a
{
public:
     a()
     {
          pointerMap.insert(pair<std::string, void a::*(int, int)> ("func1", func1);
          pointerMap.insert(pair<std::string, void a::*(int, int)> ("func2", func2);
     }

private:
     void func1(int a, int b);
     void func2(int a, int b);
     std::map<std::string, void a::* (int, int)> pointerMap;
}

My question is, is this the right way to go about adding pointers to member functions to a map within an object, so that you are only referencing the individual instance’s func1 or func2?

And also, I have no clue how I would go about calling this function from the pointer. Would it be something like this?

map["func1"](2,4);

I’m a little confused about the syntax when working with member functions.

  • 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-06-01T07:33:10+00:00Added an answer on June 1, 2026 at 7:33 am

    First, the code:

    #include <map>
    #include <string>
    class a
    {
    public:
         a()
         {
           pointerMap["func1"] = &a::func1;
           pointerMap["func2"] = &a::func2;
         }
    
         void invoke(const std::string& name, int x, int y) {
           if(pointerMap[name])
             (this->*pointerMap[name])(x, y);
         }
    
    private:
         void func1(int a, int b) {};
         void func2(int a, int b) {};
         std::map<std::string, void (a::*)(int, int)> pointerMap;
    };
    
    int main () {
      a o;
      o.invoke("func1", 1, 2);
    }
    

    Now, for your questions:

    my question is, is this the right way to go about adding pointers to member functions to a map within an object

    I find the subscript operator [] much easier to read than the insert that you were doing.

    so that you are only referencing the individual instance’s func1 or func2.

    Pointer-to-member-function doesn’t have an instance associated with it. You bind the pointer to an instance when you invoke it. So, your map could have been a static member just as easily.

    how i would go about calling this function from the pointer.

    The syntax is either: (instance.*pointer)(args) or (class_pointer->*pointer)(args). Since you didn’t say what instance the functions should be invoked on, I assumed this. Your pointers live in the map, so we have:

    ((this)->*(this->pointerMap["func1"]))(arg1, arg2)
    

    or

    (this->*pointerMap[name])(x, y);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

ok, its a little more complicated than the question. class A { static int
My question is a little bit complicated. I want to understand how the app
this is a little bit complicated question. I want to achieve the following behaviour
I think my question is a little more complicated than I can fit in
My question is a little complicated: I am having Socket server, and I have
A small - little question. I have seen many application having buttons like following.
I have little question about how web browser retrieve webpage? I know this User
I have little question. I found this on internet: Type t = typeof(MyClass); MethodInfo[]
I have a little question to community. Does ASP.NET MVC allow to execute an
I've a little question about decoding special characters from a JSon result (in my

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.