5

Hey so I am making a map with string as the key and a member function pointer as the value. I can't seem to figure out how to add to the map, this doesn't seem to be working.

#include <iostream>
#include <map>
using namespace std;

typedef string(Test::*myFunc)(string);
typedef map<string, myFunc> MyMap;


class Test
{
private:
    MyMap myMap;

public:
    Test(void);
    string TestFunc(string input);
};





#include "Test.h"

Test::Test(void)
{
    myMap.insert("test", &TestFunc);
    myMap["test"] = &TestFunc;
}

string Test::TestFunc(string input)
{
}
7
  • 2
    A guess, but &Test::TestFunc? Commented Jan 19, 2013 at 21:47
  • Seems to fix one error in the parameters, but I still get an error for insert Commented Jan 19, 2013 at 21:51
  • 1
    @Kosmo That's because insert doesn't work that way. Commented Jan 19, 2013 at 21:54
  • What does "this doesn't seem to be working" mean? Commented Jan 19, 2013 at 21:56
  • 1
    Be specific, quote the errors. Concerning insert(), you must convert it to the correct type, which is a pair<T1 const, T2>, aka map::value_type. Commented Jan 19, 2013 at 22:04

1 Answer 1

9

See std::map::insert and std::map for value_type

myMap.insert(std::map<std::string, myFunc>::value_type("test", &Test::TestFunc));

and for operator[]

myMap["test"] = &Test::TestFunc;

You cannot use a pointer to member function without an object. You can use the pointer to member function with an object of type Test

Test t;
myFunc f = myMap["test"];
std::string s = (t.*f)("Hello, world!");

or with a pointer to type Test

Test *p = new Test();
myFunc f = myMap["test"];
std::string s = (p->*f)("Hello, world!");

See also C++ FAQ - Pointers to member functions

Sign up to request clarification or add additional context in comments.

6 Comments

+1, although because std::map<A,B>::value_type is pair<const A,B> I prefer to insert MyMap::value_type(a, b) not std::make_pair(a,b) otherwise you get pair<A,B> which has to be converted to pair<const A,B> and that conversion cannot be elided.
I'm just wondering whether passing a string literal to make_pair is supposed to work? After all, the implied template type there is char[5], not std::string or somesuch.
@doomster, it's const char[5], and in C++03 make_pair takes its arguments by value, so the array decays to a pointer in the argument list, and in C++11 the return type uses std::decay<const char[5]>::type` which also decays to a pointer. So it works perfectly.
@doomster You're right, this will be a std::pair<const char*, ...> and not std::pair<std::string, ...>. Although it works, Jonathan Wakely's version should be preferred.
Thanks got it, when trying to call the function with the key I'm having trouble, I tried: convertFunc fu = converters[input]; or map<string, convertFunc>::const_iterator it = converters.find(converter); and return fu(input); or return (*fu)(input);
|

Your Answer

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