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 <map>
#include <string>
#include <iostream>
class testClass
{
public:
void testFunc1() { std::cout << "func1\n"; }
void testFunc2() { std::cout << "func2\n"; }
};
typedef void(testClass::*runFunc)(void);
typedef std::map<int, runFunc> myMapType;
int main() {
testClass t1;
std::map<int, runFunc> myMap;
myMap.emplace(1, &testClass::testFunc1);
myMap.emplace(2, &testClass::testFunc2);
myMapType::const_iterator itr;
itr = myMap.find(2);
if (itr != myMap.end()) {
(t1.*(itr->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.