Why are function addresses in message maps generated by the class wizard written with the name of the class explicitely mentioned?
For example:
ON_BN_CLICKED(IDC_CHECK1, &CMyDlg::OnClickedSomeButton)
instead of:
ON_BN_CLICKED(IDC_CHECK1, &OnClickedSomeButton)
or even:
ON_BN_CLICKED(IDC_CHECK1, OnClickedSomeButton)
All three variants compile correctly.
It's just curiosity.
MFC uses a technique called Message Maps. A Message Map is a table that associates messages with functions. When you receive a message, MFC will go through your Message Map and search for a corresponding Message Handler.
In the implementation (. cpp) file that defines the member functions for your class, start the message map with the BEGIN_MESSAGE_MAP macro, then add macro entries for each of your message-handler functions, and complete the message map with the END_MESSAGE_MAP macro.
AFX_MANAGE_STATE structure contains global data for the module, that is, the portion of the module state that is pushed or popped and also it is used to protect the exported function in DLL.
Since Windows is a message-oriented operating system, a large portion of programming for the Windows environment involves message handling. Each time an event such as a keystroke or mouse click occurs, a message is sent to the application, which must then handle the event.
All three variants compile correctly.
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 : must explicitly qualify name of member function when taking its address
ON_BN_CLICKED(IDC_CHECK1, OnClickedSomeButton)
yields Clang : call to non-static member function without an object argument
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With