Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Function addresses in MFC message maps

Tags:

c++

mfc

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.

like image 683
Jabberwocky Avatar asked Jan 25 '18 16:01

Jabberwocky


People also ask

How does MFC message Map work?

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.

What macros end message map?

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.

Which structure is used for actual message map?

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.

What is message handling in visual programming?

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.


1 Answers

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.

like image 114
pablo285 Avatar answered Oct 03 '22 03:10

pablo285