여기 내 코드입니다.매개 변수가 필요한 함수를 호출하는 맵 키 호출 - 작동 방법
#include <map>
#include <string>
#include <algorithm>
class maptest {
public:
int doubler(int val) { return val * 2; }
int halver(int val) { return val/2; }
int negativer(int val) { return val > 0 ? -val : val; }
};
int main() {
const char* const ID[] = {"doubler", "halver", "negativer" };
int ID_SIZE = sizeof(ID)/sizeof(*ID);
//signature of maths functions
typedef int (maptest::*mathfunc)(int);
mathfunc mfuncs[] = { &maptest::doubler, &maptest::halver, &maptest::negativer};
std::map<std::string, mathfunc> mathmap;
for(int i = 0; i < ID_SIZE; ++i) {
mathmap.insert(std::make_pair(ID[i], mfuncs[i]));
}
//C2064: term does not evaluate to a function taking 1 argument
int result = *mathmap["doubler"](3);
return 0;
}
함수에 전달할 매개 변수가 없으면이 방법이 효과가 있다고 생각합니다. 하지만 어떻게 이런 식으로 매개 변수를 전달합니까?
(mt. * (mathmap [ "doubler"]))에 대한 브라케팅은 혼란 스럽습니다. 왜 외부 브라켓 팅이 필요합니까? 즉, 여기 (mt. * (mathmap [ "doubler"])))? –
@ user619818 : 필요하지 않습니다. 방금 상황을 명확하게 만들었다 고 생각했습니다. 아마 내가 틀렸어;) –
내 VS2008 컴파일러에서 필요합니다. 문제 없어. –