Class with map containing function pointers of another class

As the title, suggests, I have a class that needs to have a map of pointers to members of another class. Because the name of the class should not matter, I have made it a template class. The problem I am having is how to extract a function pointer from the map, and call it. Please consider my code:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
template<typename T>
class PointerClass
{
public:
    
    typedef double(T::*memberFunction)();
    
    void addFunction(std::string funcName, memberFunction func)
    {
        funcMap.insert(std::make_pair(funcName, func));
        
        //Lets say later in another function we extract it (put it here for convenience)
        double(T::*handler)();
        handler = funcMap["new function"];
        //How do I call it? Thought it was something like this, but because templates are involved,
        //I'm lost!
        (this->*handler)();
    }

private:

    std::map<std::string, memberFunction> funcMap;
};

class MyClass
{
public:

    MyClass()
    {
        pClass.addFunc("new function", &MyClass::getValue);
    }

    double getValue()
    {
        return 25;
    }

private:

    PointerClass<MyClass> pClass;

};


PS: Thank you for any help!
Pointer to member function alone is not sufficient to execute this function - you need an object of type T.
Ok, so should I have a void* as a member of the template class, so that I can pass the *this pointer of MyClass into it? I will probably have to define a method for that, because it is generally not a good idea to do Class1<this>class1, because the object is not fully constructed.

Any ideas?
Here's a minimal working example:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
#include <iostream>
#include <string>
#include <map>

template<typename T>
struct PointerClass
{
    typedef double(T::*memberFunction)();

    void addFunction(std::string funcName, memberFunction func)
    {
        funcMap[funcName] = func;
    }

    double operator()(std::string funcName, T & object)
    {
        typename std::map<std::string, memberFunction>::const_iterator it;

        it = funcMap.find(funcName);

        if (it == funcMap.end()) return 0;

        return (object.*it->second)();
    }

    std::map<std::string, memberFunction> funcMap;
};

struct MyClass
{
    double getValue() { return 25; }
};

int main()
{
    PointerClass<MyClass> p_class;
    MyClass my_class;

    p_class.addFunction("get value", &MyClass::getValue);

    std::cout << p_class("get value", my_class) << std::endl;
    std::cout << p_class("asdf asdf", my_class) << std::endl;

    return 0;
}
Thank you for your example. What if the function being added to the template class will be called from a callback function, like a window procedure?

1
2
3
4
5
6
7
8
9
10
11
12
13
// the following inside the template class
typedef void(T::*memberFunction)(HWND window, WPARAM wp, LPARAM lp);

LRESULT CALLBACK WindProc(HWND window, UINT msg, WPARAM wp, LPARAM lp)
{
    std::map<UINT, memberFunction>::const_iterator it;

    it = funcMap.find(funcName);

    if (it == funcMap.end()) return ::DefWindProc(...)

    return (T.*it->second)(window, wp, lp);
}


So is there a way to store *this somehow inside the template class?
rem45acp wrote:
So is there a way to store *this somehow inside the template class?

Sure. You can use a T pointer, like this:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
#include <iostream>
#include <string>
#include <map>

template<typename T>
struct PointerClass
{
    typedef double(T::*memberFunction)();

    void addFunction(std::string funcName, memberFunction func)
    {
        funcMap[funcName] = func;
    }

    double operator()(std::string funcName)
    {
        typename std::map<std::string, memberFunction>::const_iterator it;

        it = funcMap.find(funcName);

        if (it == funcMap.end()) return 0;

        return (obj_ptr->*it->second)();
    }

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

    T * obj_ptr;
};

struct MyClass
{
    double getValue() { return 25; }
};

int main()
{
    PointerClass<MyClass> p_class;
    MyClass my_class;

    p_class.obj_ptr = &my_class;

    p_class.addFunction("get value", &MyClass::getValue);

    std::cout << p_class("get value") << std::endl;
    std::cout << p_class("asdf asdf") << std::endl;

    return 0;
}

Topic archived. No new replies allowed.