如何调用传递的函数(函数指针作为参数传递给函数)
How to call function passed (function ptr passed as argument to function)
在下面的command_rq行中,没有编译,在这种情况下如何调用函数?我需要如何更改功能才能使其正常工作?
#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();
}
为什么不简单:
void doA() { command_->RequestA(); }
正确的语法是这样的:
(command_->*rq)();
但是,您的代码将无法编译,因为这些行是非法的:
void doA() { doX(command_->RequestA); }
void doB() { doX(command_->RequestA); }
void doC() { doX(command_->RequestA); }
指向成员函数的指针有一个隐含的 this 参数,您不能在调用中传递它。
您必须更改函数以获取指向 Command 对象的指针,以便传递的函数指针具有一个对象,它可以是 "called on":
void doX(Request rq,Command* cmd) {
(cmd->*rq)();
}
void doA() { doX(&Command::RequestA, command_); }
void doB() { doX(&Command::RequestB, command_); }
void doC() { doX(&Command::RequestC, command_); }
您也可以通过将函数声明为静态函数并创建指向 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();
}
在下面的command_rq行中,没有编译,在这种情况下如何调用函数?我需要如何更改功能才能使其正常工作?
#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();
}
为什么不简单:
void doA() { command_->RequestA(); }
正确的语法是这样的:
(command_->*rq)();
但是,您的代码将无法编译,因为这些行是非法的:
void doA() { doX(command_->RequestA); }
void doB() { doX(command_->RequestA); }
void doC() { doX(command_->RequestA); }
指向成员函数的指针有一个隐含的 this 参数,您不能在调用中传递它。
您必须更改函数以获取指向 Command 对象的指针,以便传递的函数指针具有一个对象,它可以是 "called on":
void doX(Request rq,Command* cmd) {
(cmd->*rq)();
}
void doA() { doX(&Command::RequestA, command_); }
void doB() { doX(&Command::RequestB, command_); }
void doC() { doX(&Command::RequestC, command_); }
您也可以通过将函数声明为静态函数并创建指向 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();
}