How can I pass a member function where a free function is expected?

后端 未结 9 1819
误落风尘
误落风尘 2020-11-22 15:10

The question is the following: consider this piece of code:

#include 


class aClass
{
public:
    void aTest(int a, int b)
    {
        prin         


        
9条回答
  •  北荒
    北荒 (楼主)
    2020-11-22 15:24

    @Pete Becker's answer is fine but you can also do it without passing the class instance as an explicit parameter to function1 in C++ 11:

    #include 
    using namespace std::placeholders;
    
    void function1(std::function fun)
    {
        fun(1, 1);
    }
    
    int main (int argc, const char * argv[])
    {
       ...
    
       aClass a;
       auto fp = std::bind(&aClass::test, a, _1, _2);
       function1(fp);
    
       return 0;
    }
    

提交回复
热议问题