如何给函数指针赋值

How to assign functor to function pointer?

本文关键字:指针 赋值 函数      更新时间:2023-10-16

一般来说,我可以将函数对象分配给函数指针吗?我想这样做:

#include <iostream>
class Foo {
    int num;
public:
    Foo(int num_par) : num(num_par) {}
    void operator()(int multiplier) {
        std::cout << multiplier * num << std::endl;
    }
};
int main() {
    typedef void(*bar)(int);
    Foo f(42);
    bar b = f; // MSVC error
               // ^ C2440: 'initializing' : cannot convert from 'Foo' to 'bar'
    b(2); // wanted to print 84
}

如果这是不可能的,我想要一个替代专门为Windows编程,我想要WindowProc包含状态信息,从数据本地设置到WinMain的身体(即不做全局变量)。分配WNDCLASSEXlpfnWndProc成员时出现问题。虽然std::function解决了上面代码片段的问题:

std::function<void(int)> sf = f;
sf(2); // prints 84

它不工作在我的WinAPI情况:

class WindowProc {
    int some_state;
public:
    void set_some_state(int par);
    LRESULT CALLBACK operator()(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam);
};
int WINAPI WinMain(HINSTANCE hInstance,
    HINSTANCE hPrevInstance,
    LPSTR lpCmdLine,
    int nCmdShow) {
    HWND hWnd;
    WNDCLASSEX wc;
    WindowProc wp;
    typedef LRESULT(CALLBACK *baz)(HWND, UINT, WPARAM, LPARAM);
    std::function<baz> std_func = wp;
    std::function<baz> std_binded_func = std::bind(&WindowProc::operator(), &wp);
    // none of these compile:
    wc.lpfnWndProc = wp;                      // #1
    wc.lpfnWndProc = wp.operator();           // #2
    wc.lpfnWndProc = &WindowProc::operator(); // #3. Wrong anyway because some_state's value is part of the instance, not the class
    wc.lpfnWndProc = std_func;                // #4. Wrong anyway because some_state's value is part of the instance, not the class
    wc.lpfnWndProc = std_binded_func;         // #5
    // do stuff...
    wp.set_some_state(some_runtime_number);
    // ...do other stuff
}

MSVC 2013 Errors:

1.)错误C2440: =: cannot convert from WindowProc to WNDPROC

2.)错误C3867: WindowProc::operator ():函数调用缺少参数列表;使用&WindowProc::operator ()创建指向成员

的指针

2 cont.)错误C2440: =: cannot convert from LRESULT (__stdcall WindowProc::* )(HWND,UINT,WPARAM,LPARAM) to WNDPROC

3.)与'2 cont.'相同

4.)错误C2440: =: cannot convert from std::function<baz> to WNDPROC

4.

我怎样才能让它工作?

一般来说,函数对象除了要调用的函数之外还包含额外的上下文信息。关于唯一可以分配给函数指针的函数对象是一个带有空捕获的lambda函数,但这实际上只是一个函数:

void (*bar)(int) = [](int x) { std::cout << x; }

如果你想通过函数指针获得一个函数对象,你需要希望函数指针有一个合适的参数,可以让用户指定,并且可以直接传递:

void call_function(int arg, void* user_data) {
    (*static_cast<std::function<void(int)>*>(userdata))(arg);
}
...
std::function<void(int)> function_object;
void (*bar)(int, void*) = &call_function;
bar(17, &function_object);

大多数接受函数指针的现代接口也接受用户数据指针,该指针可用于传递必要的上下文。显然,除了传递必要的上下文之外,可能还需要以某种方式维持作为上下文的对象的生命周期。

我不是一个Windows程序员,但我猜你正在尝试使用的API实际上有能力设置函数指针和指向一些用户数据的指针。如果没有地方传递上下文信息,则需要在函数指针本身中以某种方式编码必要的信息。例如,您可以使用引用全局对象的函数指针来获取上下文。显然,对于要传递的每个上下文,都需要一个不同的函数。

您需要以通常的方式声明一个指向成员函数的指针,然后调用它。试试这个:

#include <iostream>
class Foo {
    int num;
public:
    Foo(int num_par) : num(num_par) {}
    void operator()(int multiplier) {
        std::cout << multiplier * num << std::endl;
    }
};
int main() {
    typedef void(Foo::*bar)(int);
    Foo f(42);
    bar b = &Foo::operator(); // no more MSVC error
                              // ^ C2440: 'initializing' : cannot convert from 'Foo' to 'bar'
    (f.*b)(2); // wanted to print 84, and it does
}

您必须通过实例化来调用它,在本例中为f