如何给函数指针赋值
How to assign functor to function pointer?
一般来说,我可以将函数对象分配给函数指针吗?我想这样做:
#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
的身体(即不做全局变量)。分配WNDCLASSEX
的lpfnWndProc
成员时出现问题。虽然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
我怎样才能让它工作?
一般来说,函数对象除了要调用的函数之外还包含额外的上下文信息。关于唯一可以分配给函数指针的函数对象是一个带有空捕获的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
。
- 错误:在为指针赋值时,void值没有被忽略
- 关于 c++ 函数中指针赋值的简单问题
- 链表指针赋值为什么我们不能直接将尾巴分配给 temp 而不是尾巴>尾巴下一个
- 为什么通过指针赋值常量没有效果?
- 复制构造函数中的指针赋值
- C++ 指针赋值运算符重载(不仅是对象赋值,还有指针赋值)
- 从字符串数组打印其中一个字符串时出现空指针赋值错误
- 为字符串指针赋值
- 动态指针赋值c++
- std::wstring指针赋值C++
- C++匿名结构指针赋值
- 为指针赋值而不赋值
- 此指针赋值应生成段错误
- 指向指针赋值的指针
- 哪个指针赋值更好(C++)
- <T>unique_ptr.get() 方法在使用原始指针赋值时调用析构函数?
- 为用soap_alloc创建的std::string指针赋值
- 为什么函数头的更改会导致指针赋值不起作用
- 64位c++指针赋值
- 在函数中将指针赋值到不同的地址