C++ Passing pointer to non-static member function

前端 未结 2 808
忘掉有多难
忘掉有多难 2021-01-23 09:28

Hi everyone :) I have a problem with function pointers
My \'callback\' function arguments are:
1) a function like this: int(*fx)(int,int)
2) an int variable: int a<

2条回答
  •  予麋鹿
    予麋鹿 (楼主)
    2021-01-23 09:46

    Functions cannot be referenced this way:

    int (*function3)(int, int) = obj.*function2;
    

    You have to pass the address of the function like this:

    int (*function3)(int, int) = std::mem_fn(&A::sub, obj);
    //                           ^^^^^^^^^^^^^^^^^^^^^^^^^
    

    The expression function2 decays into a pointer-to-function which allows it to work.

提交回复
热议问题