【发布时间】:2012-11-30 09:08:36
【问题描述】:
我想在 C++ 中使用指向成员函数的指针,但它不起作用:
指针声明:
int (MY_NAMESPACE::Number::*parse_function)(string, int);
指针赋值:
parse_function = &MY_NAMESPACE::Number::parse_number;
此调用完美运行(itd 是映射元素的迭代器):
printf("%s\t%p\n",itd->first.c_str(),itd->second.parse_function);
但是这个不行:
int ret = (itd->second.*parse_function)(str, pts);
$ error: 'parse_function' was not declared in this scope
这个也没有
int ret = (itd->second.*(MY_NAMESPACE::Number::parse_function))(str, pts);
$ [location of declaration]: error: invalid use of non-static data member 'MY_NAMESPACE::Number::parse_function'
$ [location of the call]: error: from this location
我不明白为什么......
提前谢谢!!
【问题讨论】:
-
熟悉std::function可能会更好
-
请出示
parse_number和itd所属容器的声明。 -
第一个调用(在 printf 中)返回一些连贯的东西(有一个 switch 案例,其中 parsing_function 被影响到“parse_function”并且。映射的所有元素
匹配 case1打印结果为 0x2ad9d65302e0,所有匹配 case2 的元素打印结果为 0x2ad9d65303b0。 -
尝试删除
int ret = (itd->second.*parse_function)(str, pts);中的星号? -
您能否编辑您的问题以包含usable example?
标签: c++ class function pointers