见代码:
#include <iostream>
using namespace std;
struct obj {
int f(int, int) { return 0; }
void g() {
int (obj :: *ptr) (int, int) = f;
((*this).*ptr)(1, 2);
}
};
int (obj :: *ptr) (int, int) = obj :: f;
int main() {
obj * s = new obj;
obj * t = new obj;
cout << ((*s).*ptr)(1, 2) << endl;
cout << (t -> *ptr)(1, 2) << endl;
return 0;
}
在我使用的小熊猫 Dev C++ 2.23(GCC 13.1.0 C17)中,对于 (t -> *ptr)(1, 2) 给出了如下的报错信息:
[错误] expected unqualified-id before '*' token
然鹅,如果我使用其它错误的语法,它又会报错(比如 ptr(1, 2)):
[错误] must use '.*' or '->*' to call pointer-to-member function in 'ptr (...)', e.g. '(... ->* ptr) (...)'
明明都说了要用 (objection -> *ptr)(args...) 的格式来调用,怎么它自己又无法解析这种语法呢?