C++ 如何调用传递的函数(函数ptr作为参数传递给函数)

C++ 如何调用传递的函数(函数ptr作为参数传递给函数),c++,function-pointers,C++,Function Pointers,在下面没有编译的命令行中,在这种情况下如何调用函数?我需要如何更改函数才能使其工作 #include <iostream> using namespace std; struct Command { bool RequestA() { cout << "RequestA\n"; return true; } bool RequestB() { cout << "RequestB\n"; return true; } bool Requ

在下面没有编译的命令行中,在这种情况下如何调用函数?我需要如何更改函数才能使其工作

#include <iostream>
using namespace std;

struct Command {
    bool RequestA() { cout << "RequestA\n"; return true; }
    bool RequestB() { cout << "RequestB\n"; return true; }
    bool RequestC() { cout << "RequestC\n"; return true; }
};

typedef bool (Command::*Request)();

class handler {
public:
    handler(Command* cmd) : command_(cmd) { }

    // *** How to now call specific function passed?
    void doX(Request rq) { command_->rq(); }
    void doA() { doX(&Command::RequestA); }
    void doB() { doX(&Command::RequestA); }
    void doC() { doX(&Command::RequestA); }
private:
    Command* command_;
};

int main() {
    Command* pCmd = new Command;
    handler h(pCmd);
    h.doA();
}
#包括
使用名称空间std;
结构命令{
bool RequestA(){cout为什么不简单地:

void doA() { command_->RequestA(); }

正确的sytax是:

(command_->*rq)();
但是,您的代码不会编译,因为这些行是非法的:

void doA() { doX(command_->RequestA); }
void doB() { doX(command_->RequestA); }
void doC() { doX(command_->RequestA); }
指向成员函数的指针有一个隐式this参数,您不能在调用中传递它

您必须更改函数以获取指向命令对象的指针,以便传递的函数指针具有一个可以“调用”的对象:

您还可以通过将函数声明为静态并创建指向bool*函数的简单指针来绕过它

class Command
{
public:
    static bool RequestA() { cout << "RequestA\n"; return true; }
    static bool RequestB() { cout << "RequestB\n"; return true; }
    static bool RequestC() { cout << "RequestC\n"; return true; }
};

typedef bool (*Request)();

class handler
{
public:
    handler(Command* cmd) : command_(cmd) { }

    // *** How to now call specific function passed?
    void doX(Request rq) {
                            (rq)();
                        }
    void doA() { doX(command_->RequestA);}
    void doB() { doX(command_->RequestB); }
    void doC() { doX(command_->RequestC); }

private:
    Command* command_;
};


int main(){

    Command* pCmd = new Command;
    handler h(pCmd);
    h.doA();
    h.doB();
    h.doC();
}
class命令
{
公众:
静态bool RequestA(){cout RequestC);}
私人:
命令*命令;;
};
int main(){
Command*pCmd=新命令;
处理程序h(pCmd);
h、 doA();
h、 doB();
h、 doC();
}

(command_->*rq)(;
您的答案不太正确,因为处理程序类有一个到command对象的ptr成员。(command_->*rq)();doX中的语法在没有其他东西的情况下也能正常工作。哈,你说得对。它上周不会在我的机器上编译,但现在可以了。我一定是改变了什么东西来破坏它,但天知道它是什么。
class Command
{
public:
    static bool RequestA() { cout << "RequestA\n"; return true; }
    static bool RequestB() { cout << "RequestB\n"; return true; }
    static bool RequestC() { cout << "RequestC\n"; return true; }
};

typedef bool (*Request)();

class handler
{
public:
    handler(Command* cmd) : command_(cmd) { }

    // *** How to now call specific function passed?
    void doX(Request rq) {
                            (rq)();
                        }
    void doA() { doX(command_->RequestA);}
    void doB() { doX(command_->RequestB); }
    void doC() { doX(command_->RequestC); }

private:
    Command* command_;
};


int main(){

    Command* pCmd = new Command;
    handler h(pCmd);
    h.doA();
    h.doB();
    h.doC();
}