Learning "in-depth and shallow MFC" has been a period of time, there are some ideas to remember, hope to share with you.
We know that Static data members in C represent a copy of the Static data member based on all objects created by this class. Based on this principle, a variety of technologies can be implemented. For example, create a counter for a certain type to record how many such objects have been created at any time.
Class test {public: test () {count ;} static int count;}; test :: count = 0; void main () {cout << Test :: count; test; t1; cout << Test :: count; test; test T2, T3; COUT << Test :: count;} Although the global variable can also achieve the purpose, it will bring name pollution and other problems, some of which will involve object-oriented concepts.
In MFC, Static has been widely used. Houjie analyzes the principles of MFC in writing "in-depth light-out MFC" and various technologies such as dynamic creation, runtime type identification, message mapping, command delivery, etc., all and STATIC is inseparable. The following is to discuss the application of STATIC as an example in the "message mapping".
About static, an important concept is: Static member variable is not because the object is implemented, it exists, you can imagine it is a global variable. (See "In-depth MFC" P71 page). So let's see the macro definition of DECLARE_MESSAGE_MAP:
#define declare_message_map () / static AFX_MSGMAP_ENTRY _MESSAGEENTRIES []; / static afx_msgmap messageMap; / virtual afx_msgmap * getMessageMap () const;
If we define a class CMYWND that is derived from CFrameWnd and uses DECLARE_MESSAGE_MAP macro
// Class CMyWnd: public cframewnd {public: ... declare_MESSAGE_MAP (); ......};
In fact, two Static data members (and a virtual function) are declared for this class, followed by the begin_MESSAGE_MAP macro
/ / In myApp.cpp file becom_message_map (cmyWnd, cframewnd) ON_WM_PAINT () end_MESSAGE_MAP ()
In fact, it defines two variables that define CMYWnd :: MessageMap and CMYWnd :: _ MessageEntries and give them appropriate initial values. At this point, although a CMYWND object has not been generated, the two variables are exist. In fact, before entering the AFXWINMAIN function (provided by MFC), not only these two variables exist, other variables related to some important classes of the MFC, such as cwinapp :: messagemap and cwinapp :: Messagentries, CWnd: : MessageMap and CWnd :: MessageEntries, etc., which constitute a message delivery network, providing an important basis for the flow of the subsequent messages. Of course, this will take up part of memory.