1

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;
  };
2
  • As an aside, will you also want to store the object's this pointer of the objects to whose member functions you want to store pointers? Commented Jun 20, 2011 at 16:57
  • @KerrekSB As it turns out, that is also what I need. I'm sorry I didn't see this reply earlier. Commented Jun 22, 2011 at 2:51

1 Answer 1

5

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;
}
Sign up to request clarification or add additional context in comments.

4 Comments

Wow, I was expecting an answer full of pointers everywhere, structs, and macros and stuff like that. I forget how templates solve so much headache. Thank you so much!
One more thing, if you don't mind: How would I call a function pointer from MyClass inside Class1? Lets say I retrieve it out of the map: double(T::*memFunc)(); memFunc = (*mapIterator).second; (this->*memFunc)(); That does not seem to work.
@rem45acp if memFunc is a function pointer from MyClass, it needs an object of type MyClass to be called on: ideone.com/YAHSG
Could *this be passed into the template, like so: Class1<*this>class1, avoiding the need for the extra parameter?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.