开发者

warning C4800: 'int' : forcing value to bool 'true' or 'false' (performance warning) in MFC(VC++)

开发者 https://www.devze.com 2023-01-31 01:20 出处:网络
I am getting this warning \"warning C4800: \'int\' : forcing value to bool \'true\' or \'false\' (performance warning)\" on below line of code 开发者_StackOverflow社区:

I am getting this warning "warning C4800: 'int' : forcing value to bool 'true' or 'false' (performance warning)" on below line of code 开发者_StackOverflow社区:

if (g_objConfigurationDetails.bAutoScan !=  m_objScanNASBtn.GetCheck())
        {
            g_objConfigurationDetails.bAutoScan = m_objScanNASBtn.GetCheck();
}

both the lines are giving the same warning. where bAutoScan = bool m_objScanNASBtn = CButton


The reason is that MFC's GetCheck doesn't return a bool:

The return value from a button control created with the BS_AUTOCHECKBOX, BS_AUTORADIOBUTTON, BS_AUTO3STATE, BS_CHECKBOX, BS_RADIOBUTTON, or BS_3STATE style is one of the following values:
- BST_UNCHECKED - Button state is unchecked.
- BST_CHECKED - Button state is checked.
- BST_INDETERMINATE - Button state is indeterminate (applies only if the button has the BS_3STATE or BS_AUTO3STATE style).

So you probably want

if (g_objConfigurationDetails.bAutoScan
      != (m_objScanNASBtn.GetCheck() == BST_CHECKED))

or

bool bNASBtnChecked = (m_objScanNASBtn.GetCheck() == BST_CHECKED);
if (g_objConfigurationDetails.bAutoScan != bNASBtnChecked)
{
    g_objConfigurationDetails.bAutoScan = bNASBtnChecked;
}


The following:

g_objConfigurationDetails.bAutoScan = m_objScanNASBtn.GetCheck();

should be

g_objConfigurationDetails.bAutoScan = m_objScanNASBtn.GetCheck() == BST_CHECKED;

The problem is GetCheck() doesn't return bool so you have to write a comparison - at least in the form of != 0.


GetCheck returns an integer and you're comparing it to a boolean. Try comparing GetCheck to BST_UNCHECKED or BST_CHECKED depending on your boolean value, or just cast it to boolean since the 2 values match true and false numerically.

0

精彩评论

暂无评论...
验证码 换一张
取 消