微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

从结构调用C成员函数指针

我已经找到了有关调用C成员函数指针和在结构中调用指针的信息,但我需要调用一个存在于结构内部的成员函数指针,并且我无法使语法正确.我在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);')

移动这些括号导致语法错误,建议使用.*或 – > *这两种情况都不起作用.有谁知道正确的语法?

解决方法

使用:
(this->*functionMap[0].command)(x,y);

经过测试和编译;)

原文地址:https://www.jb51.cc/c/116723.html

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐