我正在努力将会员的地址传递给另一个职能部门。 这就是我想要做的事情:
我有以下防守:
bool MyClass::FunctionName();
然后在我的程序中的某个地方执行以下命令:
::SendMessage(hWnd, WM_NULL, (WPARAM)this, (LPARAM)&MyFunction);
其中此代表MyClass
一旦我的WndProc被执行,我试试这个:
LRESULT CALLBACK MyClass::WndProc(_In_ HWND hWnd, _In_ UINT uMsg, _In_ WPARAM wParam, _In_ LPARAM lParam)
{
Myclass* pClass= (Myclass*)wParam;
std::function<bool()> pFunc = std::bind(bool(&Myclass::MyFunction)&lParam, pClass);
pFunc();
}
错误即将收到:
警告C4554:&#39;&amp;&#39; :检查运算符优先级是否存在错误;使用 括号以澄清优先顺序
错误3错误C2064:术语不评估为1的函数 参数c:\ program files(x86)\ microsoft visual studio 12.0 \ vc \ include \ xrefwrap 58
答案 0 :(得分:0)
这应该编译:
LRESULT CALLBACK MyClass::WndProc(_In_ HWND hWnd, _In_ UINT uMsg,
_In_ WPARAM wParam, _In_ LPARAM lParam)
{
switch (uMsg)
{
case WM_METHOD_CALL:
Myclass* pClass= static_cast<Myclass*>(wParam);
typedef bool Myclass::pmf();
const auto pointer_to_member_function = static_cast<pmf>(lParam);
(pClass->*pointer_to_member_function)();
break;
}
}
你的问题是虽然期望WPARAM成为类指针是完全安全的,但是“指向成员函数的指针”是一个令人惊讶的复杂的野兽(考虑在第二个基类中的虚函数),并且通常更大比一个指针 - 所以它不适合LPARAM。
如果您想使用SendMessage
,则可以执行以下操作:
将typedef移动到Myclass:
typedef bool Myclass::pmf(); // Inside the definition of Myclass.
您计划中的某个地方:
Myclass::pmf ptr = &MyFunction;
::SendMessage(hWnd, WM_METHOD_CALL, (WPARAM)this, (LPARAM)&ptr);
然后在你的消息处理程序中:
LRESULT CALLBACK MyClass::WndProc(_In_ HWND hWnd, _In_ UINT uMsg,
_In_ WPARAM wParam, _In_ LPARAM lParam)
{
Myclass* pClass= static_cast<Myclass*>(wParam);
switch(uMsg)
{
case WM_METHOD_CALL:
do_method_call(wParam, lParam);
break;
}
return 0; // Or whatever.
}
void do_method_call(_In_ WPARAM wParam, _In_ LPARAM lParam)
{
const auto pointer_to_member_function = *static_cast<Myclass::pmf*>(lParam);
(pClass->*pointer_to_member_function)();
}