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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T06:19:12+00:00 2026-05-23T06:19:12+00:00

What I wish to do is have a class that contains a map of

  • 0

What I wish to do is have a class that contains a map of function pointers of a second class, but the name of the second class should not matter (cannot be hard coded into the first class) I would really like to be able to implement this WITHOUT using macros. I have followed the examples from learncpp.com on function pointers, but when passing them between classes I am really lost! My attempt is below:

#include <map>
class Class1;
typedef double(Class1::*memFunc)();

class Class1
{
  private:

  std::map<std::string, memFunc> funcMap;

  public:

  void addFunc(std::string funcName, memFunc function)
  {
      funcMap.insert(std::pair<std::string, memFunc>(funcName, function));
  }

  };

  class MyClass
  {
  public:

  MyClass()
  {
      //How do I add member function getValue() to Class1?
      class1.addFunc("new function", getValue());
  }

  double getValue()
  {
      return 0;
  }

  private:

  Class1 class1;
  };
  • 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-23T06:19:12+00:00Added an answer on May 23, 2026 at 6:19 am

    The name of the class is a part of the type of the function pointer, which becomes part of the map’s type, which becomes part of MyClass. Depending on how strong is the requirement “cannot be hard coded”, perhaps a template would be sufficient?

    #include <string>
    #include <map>
    template<typename T>
    class Class1
    {
        typedef double(T::*memFunc)();
        std::map<std::string, memFunc> funcMap;
     public:
        void addFunc(std::string funcName, memFunc function)
        {
            funcMap.insert(std::pair<std::string, memFunc>(funcName, function));
        }
    };
    
    class MyClass
    {
     public:
        MyClass()
        {
            class1.addFunc("new function", &MyClass::getValue);
        }
        double getValue()
        {
            return 0;
        }
    private:
        Class1<MyClass> class1;
    };
    int main()
    {
         MyClass mc;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

We have a vendor-provided Jar that contains a class we wish to extends and/or
I have a class that I wish to expose as a remote service using
I have a class that includes a std::list and wish to provide public begin()
Here is the domain that I wish to have: public class Person { public
Assume I have a class foo, and wish to use a std::map to store
So lets say I have a class called Post that contains an IList I
I have a custom class that I wish to save and load. The class
If I have a non-template (i.e. normal) class and wish to have a template
I have a DateTime class and wish to display it according to some format...
Suppose I have a base class B, and a derived class D. I wish

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.