本文介绍了从结构调用C ++成员函数指针的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我已经找到关于调用C ++成员函数指针和在结构体中调用指针的信息,但是我需要调用存在于结构体中的成员函数指针,并且我还没有得到正确的语法。我在类MyClass的方法中有以下代码片段:
I have found information on calling C++ member function pointers and calling pointers in structs, but I need to call a member function pointer that exists inside of a structure, and I have not been able to get the syntax correct. I have the following snippet inside a method in class MyClass:
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);')
移动这些括号会导致语法错误,推荐使用。*或 - > *,在这种情况下都不起作用。是否有人知道正确的语法?
Moving those parenthesis around results in syntax errors recommending using .* or ->* neither of which work in this situation. Does anyone know the proper syntax?
推荐答案
使用:
(this->*functionMap[0].command)(x, y);
测试并编译;)
这篇关于从结构调用C ++成员函数指针的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!