【发布时间】:2011-10-15 06:40:58
【问题描述】:
我找到了有关调用 C++ 成员函数指针和调用结构中的指针的信息,但是我需要调用结构内部存在的成员函数指针,并且我无法获得正确的语法。我在 MyClass 类的方法中有以下 sn-p:
void MyClass::run() {
struct {
int (MyClass::*command)(int a, int b);
int id;
} functionMap[] = {
{&MyClass::commandRead, 1},
{&MyClass::commandWrite, 2},
};
(functionMap[0].MyClass::*command)(x, y);
}
int MyClass::commandRead(int a, int b) {
...
}
int MyClass::commandWrite(int a, int b) {
...
}
这给了我:
error: expected unqualified-id before '*' token
error: 'command' was not declared in this scope
(referring to the line '(functionMap[0].MyClass::*command)(x, y);')
移动这些括号会导致语法错误,建议使用 .* 或 ->* 在这种情况下两者都不起作用。有谁知道正确的语法吗?
【问题讨论】:
-
stackoverflow.com/questions/990625/… 似乎与这个问题有关。
标签: c++ function-pointers