How to pass a method as parameter?

后端 未结 2 1116
南笙
南笙 2020-12-31 22:32

Having this class :

class Automat
{
private:
    // some members ... 
public:
    Automat();
    ~Automat();
    void addQ(string& newQ) ; 
    void addC         


        
2条回答
  •  囚心锁ツ
    2020-12-31 22:47

    How would you call it? C++ is not a dynamically typed language; it is statically typed. Therefore, everything you call must have a specific set of parameters, and each parameter must be typed. There's no way to call "some function" with some number of parameters and hope that it can be sorted out at runtime.

    You need a specific interface. methodToDo needs to have some kind of interface; without one, you cannot call it.

    The best you might be able to do is to have multiple versions of handleStringSituations, where each one takes a different member pointer type:

    void handleStringSituations(string &stringOfSituation , Automat* autoToHandle ,void (Automat::*methodToDo) ()) ;
    void handleStringSituations(string &stringOfSituation , Automat* autoToHandle ,void (Automat::*methodToDo) (string&)) ;
    void handleStringSituations(string &stringOfSituation , Automat* autoToHandle ,void (Automat::*methodToDo) (Lamda&)) ;
    

提交回复
热议问题