Pass a class member function as a function parameter

前端 未结 3 1605
慢半拍i
慢半拍i 2020-12-31 19:34

I have 2 C++ Class questions:

The 1st question is: How can I make it so I can pass a class member function as a parameter in another function &

3条回答
  •  囚心锁ツ
    2020-12-31 20:00

    In C++ 11 they came up with a way to do that. Read about function and bind operations.

    In your case, let's say you wanted to call functions of type test1. (i.e. of form bool FunctionName().

    void catalogueTest( string testName, std::function myFunction)
    {
        testLog += "Status of " + testName + ": " + myFunction() + "\n"; 
    }
    

    And call it like this:

    DebuggingManager myInstance
    myInstance->catalogueTest("TestName", std::bind(&DebuggingManager::test1, myInstance));
    

提交回复
热议问题