Qing Dujun
Address: http://blog.csdn.net/qingdujun/article/details/35263857
In MFC, a class requires a member variable value of another class object, which requires obtaining the pointer of the original class object. In fact, there are several methods that can be implemented.
For example, maintain a singleton mode and set static variables. Here we will give you a column to implement mutual question between multiple classes.
I. Demo: Create an MFC dialog box to renew the data in the dialog box
- Create an MFC dialog box application named visitproject.
- The dialog box itself has a main interface (cvisitdlg dialog box), so let's add it again? A new interface cxxxdlg.
2. In the header file of the cvisitdlg class on the main interface, add? A static cvisitdlg * s_pdialog; pointer (Note: add? For the shared ownership type)
Because this pointer is static and needs to be initialized outside the class, we will write a line of initialization code in the cvisitdlg. cpp file, and the initialization is empty directly, for example:
Cvisitdlg * cvisitdlg: s_pdialog = NULL; // (do not write it in the class implementation function unless the class is written ).
3. assign a value to the pointer of the Main Dialog box when the cvisitdlg Main Dialog Box is generated.
Therefore, we can write the following code in the constructor of the cvisitdlg class or oninitdialog () function:
S_pdialog = this;
Well, now we only need to obtain this static pointer in other classes, so we can ask all the data in this class ~~~~
4. Obtain static pointers
If we want to query the data of the cvisitdlg class in the cxxxdlg class, the following code can be easily done:
Cvisitdlg * pdia = cvisitdlg: s_pdialog;