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

后端 未结 9 1771
误落风尘
误落风尘 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:29

    I made the member function as static and all works:

    #include 
    
    class aClass
    {
    public:
        static void aTest(int a, int b)
        {
            printf("%d + %d = %d\n", a, b, a + b);
        }
    };
    
    void function1(int a,int b,void function(int, int))
    {
        function(a, b);
    }
    
    void test(int a,int b)
    {
        printf("%d - %d = %d\n", a , b , a - b);
    }
    
    int main (int argc, const char* argv[])
    {
        aClass a;
    
        function1(10,12,test);
        function1(10,12,a.aTest); // <-- How should I point to a's aClass::test function?
    
        getchar();return 0;
    }
    

提交回复
热议问题