我想用一個指向C++成員函數,但它不工作: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
我不understant爲什麼...
Thx提前!
可能會更好,以熟悉的std ::功能 – goji
請出示parse_number'的'聲明的容器,並itd'所屬的'。 – Angew
第一次調用(在printf中)返回相關的東西(有一個開關情況,其中parsing_function受到「parse_function」影響,並且。與case1匹配的地圖的所有元素的打印結果爲0x2ad9d65302e0,所有與case2匹配的元素的打印結果是0x2ad9d65303b0。 –
Jav