问题描述
以这个简单的代码为例:
void CRestoreSettingsDlg::OnSize(UINT nType,int cx,int cy)
{
CResizingDialog::OnSize(nType,cx,cy);
m_gridBackupLog.ExpandLastColumn();
}
为什么会被标记?
C26434 函数 'CRestoreSettingsDlg::OnSize'
隐藏了非虚拟函数 'CRestoreDialogDlg::OnSize'
。
声明和定义
-
CRestoreSettingsDlg
:
public:
afx_msg void OnSize(UINT nType,int cy);
void CRestoreSettingsDlg::OnSize(UINT nType,cy);
m_gridBackupLog.ExpandLastColumn();
}
-
CResizingDialog
:
public:
afx_msg void OnSize(UINT nType,int cy);
void CResizingDialog::OnSize(UINT nType,int cy)
{
CDialogEx::OnSize(nType,cy);
Invalidate(TRUE);
}
- 样板基类 (
afxwin.h
) 似乎具有:
protected:
afx_msg void OnSize(UINT nType,int cy);
_AFXWIN_INLINE void CWnd::OnSize(UINT,int,int)
{ Default(); }
继承
class CRestoreSettingsDlg : public CResizingDialog
class CResizingDialog : public CDialogEx
解决方法
C26434 warning documentation 链接到 C.128 C++ Core Guidelines Rule。它解释了为了强制正确使用虚函数,非虚函数隐藏应该产生警告。
但是,对于 MFC 消息映射,您必须按照宏中指定的方式命名消息处理程序,在本例中为 OnSize
,并且由于消息处理程序已经由虚拟函数(隐藏在 {{1 }} 宏),消息处理程序本身不必是虚拟的。
因此它可能被视为误报。或者可能被 MFC 视为违反了上述 C.128 规则。毫不奇怪 - MFC 比这些指南早了几十年。
所以我想你可以继续为所有 *_MESSAGE_MAP()
函数抑制它。也许重新定义 afx_msg
以包含 afx_msg
,或者只是在 __pragma(warning(suppress(...)))
块周围进行抑制。
一些抑制选项 (Godbolt's compiler explorer demo):
afx_msg