MFC 消息映射中的函数地址
为什么类向导生成的消息映射中的函数地址写有明确提到的类名?
Why are function addresses in message maps generated by the class wizard written with the name of the class explicitely mentioned?
例如:
ON_BN_CLICKED(IDC_CHECK1, &CMyDlg::OnClickedSomeButton)
代替:
ON_BN_CLICKED(IDC_CHECK1, &OnClickedSomeButton)
甚至:
ON_BN_CLICKED(IDC_CHECK1, OnClickedSomeButton)
所有三个变体都能正确编译.
All three variants compile correctly.
只是好奇.
推荐答案
所有三个变体都能正确编译.
All three variants compile correctly.
是的,它们可以正确编译...在 MSVC 上.如果您尝试在 Clang(Microsoft 兼容模式)中编译第二个或第三个示例,则会出现错误.
Yes, they compile correctly...on MSVC. If you try to compile the second or third example in Clang (Microsoft compatibility mode) you will get an error.
ON_BN_CLICKED(IDC_CHECK1, &OnClickedSomeButton)
yields Clang : 获取地址时必须明确限定成员函数的名称
ON_BN_CLICKED(IDC_CHECK1, OnClickedSomeButton)
产生 Clang : 在没有对象参数的情况下调用非静态成员函数
即使您不想发布 MSVC 编译的二进制文件,也最好坚持第一种形式 - 您可能想使用需要 clang 编译的 clang-tidy 等工具.这也是唯一的 C++ 标准符合方式传递成员函数指针.另外两个由微软的扩展工作.
It's definitely better to stick to the first form even if you never want to ship other than MSVC-compiled binaries - you might want to use tools like clang-tidy which require clang compilation. It is also the only C++ standard-conformant way to pass a member function pointer. The other two work by Microsoft's extension.
相关文章