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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T07:31:32+00:00 2026-05-30T07:31:32+00:00

I would create an interface like that : class IMother { public: // This

  • 0

I would create an interface like that :

class IMother {
public:
  // This getter return a map which contains a member functer pointer
  virtual map<string, void (IMother::*)()> getMap() const = 0;
  virtual ~IModule() {};
};

Then, create a child and override the getter in order to return a map which contain only Child_1 member function pointer

class Child_1 : public IMother {
private:
  map<string, void (Child1::*)(int)> _map;

public:
  void do_something_1(int a) {
     // Something...
  }

  void do_something_2(int a) {
   // Something...
  }

  virtual map<string, void (Child1::*)(int)> getMap() {
     _map["do_1"] = &do_something_1;
     _map["do_2"] = &do_something_2;
     return _map;
  }

I thought I’ll be able to make it work because, in my mind, I thought Child1 is a IMother so I have the right to write that but I cannot..

int main() {
   IMother *mother = new Child_1;

   // I don't know how run a method through a map
   mother->getMap["do_1"](42); // Not seem to work
   return 0;
}

Is there a way to store a member function pointer trough an interface ?

  • 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-30T07:31:33+00:00Added an answer on May 30, 2026 at 7:31 am

    There are few issues here:

    1. Firstly assignment of pointer to members is incorrect:

      This:

      _map["do_1"] = &do_something_1;
      _map["do_2"] = &do_something_2;
      

      Should be:

      _map["do_1"] = &Child1::do_something_1;
      _map["do_2"] = &Child1::do_something_2;
      
    2. Second, the return types of getMap() on IMother and Child1 differ since one takes no params and a pointer to member of IMother and the other takes an int and is a pointer to member of Child1. Those two differences cause the return types to be different in C++.

      IMother:

      map<string, void (IMother::*)()>  
      

      Child1:

      map<string, void (Child1::*)(int)>
      

      Since the return types are different Child1 has not overridden all the pure virtual functions defined in IMother, thus you cannot instantiate an instance of Child1.

    3. Third, your member function invocation is incorrect. Member functions still need the “member” supplied before invoking. e.g.

      class SomeClass;
      typedef void (SomeClass::* SomeClassFunction)(void);
      
      void Invoke(SomeClass *pClass, SomeClassFunction funcptr) {
          (pClass->*funcptr)(); 
      };
      

    That being said I would look at the stl functional header. stl functional lib will allow you to write the following and later invoke them in a much simpler syntax than the built in C++ syntax:

    typedef std::function<float (float)> MyFuncType;
    
    map<sting, MyFuncType> myFuncs;
    myFuncs["myCustomSin"] = &SomeClass::SomeCustomSinFunc;
    myFuncs["cSin"] = &sin;
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I would like to create a interface that I can use with different types.
I would like to create a user interface like the iGoogle or facebook profiles
I'd like to know how one would create an application that starts in the
How does the code looks that would create an object of class: string myClass
I would like create my own collection that has all the attributes of python
Similar to this Java question . I would like to specify that a variable
I'd like to write a MessageConverter class that can wrap another MessageConverter. This MessageConverter
OK, I know how to create a class extension, using something like that: on
I'm implementing a REST-style interface and would like to be able to create (via
I normally would create a limited rights user and run the process under that

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.