Improvement for std::map  double dispatch I am trying to do my c++ homework and as a part of it I have to implement a std::map with an int as its key and a value should be a specific void function, which prints some text.
I can`t find an answer to what are the correct ways of doing that.

This is what I have so far:

#include &lt;map&gt;
#include &lt;string&gt;
#include &lt;iostream&gt;

class testClass
{
public:
    void testFunc1() { std::cout &lt;&lt; "func1\n"; }
    void testFunc2() { std::cout &lt;&lt; "func2\n"; }
};

typedef void(testClass::*runFunc)(void);
typedef std::map&lt;int, runFunc&gt; myMapType;

int main() {
    testClass t1;

    std::map&lt;int, runFunc&gt; myMap;
    myMap.emplace(1, &amp;testClass::testFunc1);
    myMap.emplace(2, &amp;testClass::testFunc2);

    myMapType::const_iterator itr;
    itr = myMap.find(2);

    if (itr != myMap.end()) {
        (t1.*(itr-&gt;second))();
    }
}


The code is working now, however I am not sure if that is the way people with more experience do. 

Also, if you are aware of any sources where one can read more about knowledge required to achieve the needed result - any ideas are appreciated.
In general your code looks good. But I have some little annotations (improvements) for you.

1. I'd prefer direct initialization of itr for sake of readability:

myMapType::const_iterator itr = myMap.find(2);


instead of 

myMapType::const_iterator itr;
itr = myMap.find(2);


That said you could also avoid 

typedef std::map&lt;int, runFunc&gt; myMapType;


completely using the auto keyword:

auto itr = myMap.find(2);


2. Make use std::function and lambda bindings

For the callable value parameter of the std::map I'd prefer to use an appropriate std::function value type and lambda function bindings to the instance to operate on.

testClass t1;

std::map&lt;int, std::function&lt;void()&gt;&gt; myMap;
myMap.emplace(1, [&amp;t1](){t1.testFunc1();});
myMap.emplace(2, [&amp;t1](){t1.testFunc2();});


This will give you greater flexibility for later changes and use of other classes than just testClass. Also you can get rid of that other type definition then:

typedef void(testClass::*runFunc)(void);


As soon you want to use your map for the general case you can clearly see the benefits:

class A {
public:
     void print() { std::cout &lt;&lt; "print() from class A.\n"; }
};

class B {
public:
     void print() { std::cout &lt;&lt; "print() from class B.\n"; }
};

// ...
int main() {
    A a;
    B b;

    std::map&lt;int, std::function&lt;void()&gt;&gt; myMap;
    myMap.emplace(1, [&amp;a](){a.print();});
    myMap.emplace(2, [&amp;b](){b.print();});

}




Here's my set of changes in whole (you can check it's still working as intended here): 

#include &lt;map&gt;
#include &lt;string&gt;
#include &lt;iostream&gt;
#include &lt;functional&gt;

class testClass
{
public:
    void testFunc1() { std::cout &lt;&lt; "func1\n"; }
    void testFunc2() { std::cout &lt;&lt; "func2\n"; }
};

int main() {
    testClass t1;

    std::map&lt;int, std::function&lt;void()&gt;&gt; myMap;
    myMap.emplace(1, [&amp;t1](){t1.testFunc1();});
    myMap.emplace(2, [&amp;t1](){t1.testFunc2();});

    auto itr = myMap.find(2);

    if (itr != myMap.end()) {
        (itr-&gt;second)();
    }
}