About where to initialize the variables in the dialog box application

Source: Internet
Author: User

I suggest you initialize the member variables in the oninitdialog () function, and try not to initialize them in the constructor.

For example, create a dialog box application and generate a "ccalculatordlg" class, which inherits from the "cdialog" class.

This issue is explained based on the "ccalculatordlg" class:

The ccalculatordlg class defines two member variables: Double m_data and cedit * m_pedit.

1. Both variables can be initialized in the ccalculatordlg () constructor or oninitdialog () function as follows:

M_data = 0;

M_pedit = NULL;

2. You can perform the following initialization in the oninitdialog () function:

M_data = 0;

M_pedit = (cedit *) This-> getdlgitem (idc_edit); // idc_edit is the cedit Control ID in the dialog box.

3. perform the following initialization in the ccalculatordlg () constructor:

M_pedit = (cedit *) This-> getdlgitem (idc_edit); // idc_edit is the cedit Control ID in the dialog box.

An error is reported when the "Debug solution" is generated, but no error is reported when the "Release solution" is generated.

Error reported when "Debug solution" is generated:

Debug assertion failed!

Program :...

File:... \ winocc. cpp

Line: 92

Open winocc. cpp and jump to line 1

 Cwnd * cwnd: getdlgitem (int nid) const <br/>{< br/> assert (: iswindow (m_hwnd )); // The value is 92nd rows. </P> <p> If (m_pctrlcont = NULL) <br/> return cwnd: fromhandle (: getdlgitem (m_hwnd, NID )); <br/> else <br/> return m_pctrlcont-> getdlgitem (NID); <br/>}

This statement asserted whether m_hwnd is a form handle. It only executes this statement when "Debug solution" is generated, and skips this macro when "Release solution" is generated.

As to why it reports an error, I understand it as follows: In the generate ccalculatordlg dialog box and call ccalculatordlg () m_hwnd in the base class cwnd of the constructor is too late for initialization (I .e. m_hwnd = NULL). Therefore, an error will occur when you execute the 92nd row assertions, therefore, it is best to put the initialization of this class in oninitdialog.

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.