MFC: A class member asks about the member variable value of a class member object.

Source: Internet
Author: User

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

  1. Create an MFC dialog box application named visitproject.
  2. 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;


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.