std::function -> function pointer

后端 未结 2 1546
暗喜
暗喜 2020-12-19 00:54

Here is a code:

#include 
using namespace std::tr1;

typedef void(*fp)(void);

void foo(void)
{

}

void f(fp)
{

}

int main()
{
  functio         


        
2条回答
  •  执笔经年
    2020-12-19 01:16

    You've greatly oversimplified your real problem and turned your question into an XY problem. Let's get back to your real question: how to call SetupIterateCabinet with a non-static member function as a callback.

    Given some class:

    class MyClass
    {
    public:
        UINT MyCallback(UINT Notification, UINT_PTR Param1, UINT_PTR Param2)
        {
            /* impl */
        }
    };
    

    In order to use MyClass::MyCallback as the third argument to SetupIterateCabinet, you need to pass a MyClass* for the Context argument and use a plain shim function to take that Context argument and do the right thing with it:

    UINT MyClassCallback(PVOID Context, UINT Notification, UINT_PTR Param1, UINT_PTR Param2)
    {
        return static_cast(Context)->MyCallback(Notification, Param1, Param2);
    }
    
    int main()
    {
        MyClass mc;
        SetupIterateCabinet(_T("some path"), 0, MyClassCallback, &mc);
    }
    

提交回复
热议问题