下面是我的代碼傳遞字符串作爲參考爲一個函數指針C++
#include "stdafx.h"
#include <string.h>
#include <iostream.h>
using namespace std;
class ToDoCommands
{
public:
void getCommand(string);
};
void ToDoCommands::getCommand(string command)
{
cout<<command; //here i get ping
void (*CommandToCall)(void);
CommandToCall = command; // error here i want something like
// CommandToCall = ping
CommandToCall();
}
void ping(void)
{
cout<<"ping command executed";
}
int main()
{
ToDoCommands obj;
obj.getCommand("ping");
}
函數指針應參考函數平動態。與函數名稱相同的字符串被傳遞給main中的getCommand函數。
C++少了點這樣工作。如果你確實需要這樣的東西,你必須創建一個按名稱索引的函數表。 –