有这个课程:
class Automat
{
private:
// some members ...
public:
Automat();
~Automat();
void addQ(string& newQ) ;
void addCharacter(char& newChar) ;
void addLamda(Lamda& newLamda) ;
void setStartSituation(string& startQ) ;
void addAccQ(string& newQ) ;
bool checkWord(string& wordToCheck) ;
friend istream& operator >> (istream &isInput, Automat &newAutomat);
string& getSituation(string& startSituation) ;
};
Run Code Online (Sandbox Code Playgroud)
并且所谓的类 Menu具有以下方法:
void Menu::handleStringSituations(string &stringOfSituation , Automat* autoToHandle ,void (Automat::*methodToDo) () )
{
// some code ...
(*autoToHandle).*methodToDo() ;
}
Run Code Online (Sandbox Code Playgroud)
该行 (*autoToHandle).*methodToDo() ;给出了错误.
正如您所看到的,我尝试将Automat类中的任何方法作为参数传递给handleStringSituations方法但没有成功.
您尝试做的事情通常称为闭包,这是函数式编程中的一个重要概念。我建议您不要重新发明轮子,而是研究一下 Boost::Phoenix,它在一个很好的、经过同行评审的库中提供了这一点。
http://www.boost.org/doc/libs/1_47_0/libs/phoenix/doc/html/index.html
然而,由于 C++ 是一种静态类型语言,因此您必须进行一些编组。C++ 中不存在泛型函数(对象)之类的东西。