C26434 函数 xxx 隐藏了一个 non-virtual 函数
C26434 Function xxx hides a non-virtual function
使用这个简单的代码:
void CRestoreSettingsDlg::OnSize(UINT nType, int cx, int cy)
{
CResizingDialog::OnSize(nType, cx, cy);
m_gridBackupLog.ExpandLastColumn();
}
为什么它被标记了?
C26434 Function 'CRestoreSettingsDlg::OnSize'
hides a non-virtual function 'CRestoreDialogDlg::OnSize'
.
如你所见,我调用了基础class方法。
声明和定义
CRestoreSettingsDlg
:
public:
afx_msg void OnSize(UINT nType, int cx, int cy);
void CRestoreSettingsDlg::OnSize(UINT nType, int cx, int cy)
{
CResizingDialog::OnSize(nType, cx, cy);
m_gridBackupLog.ExpandLastColumn();
}
CResizingDialog
:
public:
afx_msg void OnSize(UINT nType, int cx, int cy);
void CResizingDialog::OnSize(UINT nType, int cx, int cy)
{
CDialogEx::OnSize(nType, cx, cy);
Invalidate(TRUE);
}
- 样板基础 class (
afxwin.h
) 似乎有:
protected:
afx_msg void OnSize(UINT nType, int cx, int cy);
_AFXWIN_INLINE void CWnd::OnSize(UINT, int, int)
{ Default(); }
继承
class CRestoreSettingsDlg : public CResizingDialog
class CResizingDialog : public CDialogEx
C26434 warning documentation links to C.128 C++ Core Guidelines Rule。它解释了要强制正确使用虚函数,非虚函数隐藏应该产生警告。
但是,对于 MFC 消息映射,您必须按照宏中指定的方式命名消息处理程序,在本例中为 OnSize
,并且由于消息处理程序已经由虚函数(隐藏在 *_MESSAGE_MAP()
宏),消息处理程序本身不必是虚拟的。
因此它可能被视为误报。或者可能被 MFC 本身视为违反上述 C.128 规则。毫不奇怪 - MFC 比这些准则早几十年。
所以我想您可以继续并为所有 afx_msg
函数抑制它。也许重新定义 afx_msg
以包含 __pragma(warning(suppress(...)))
,或者只是对 afx_msg
块进行抑制。
一些抑制选项(Godbolt's compiler explorer demo):
#define afx_msg // this is normally defined by MFC
struct base
{
afx_msg void OnSize(){}
};
struct derived1 : base
{
afx_msg void OnSize() {} // produces C26434
};
// Suppression by adding some code:
struct derived2 : base
{
#pragma warning(push)
#pragma warning(disable:26434)
afx_msg void OnSize() {}
#pragma warning(pop)
};
struct derived3 : base
{
[[gsl::suppress(c.128)]] afx_msg void OnSize() {}
};
// Suppression by redefining MFC macro -- dirty but less intrusive:
#undef afx_msg
#define afx_msg __pragma(warning(suppress:26434))
struct derived4 : base
{
afx_msg void OnSize() {}
};
#undef afx_msg
#define afx_msg [[gsl::suppress(c.128)]]
struct derived5 : base
{
afx_msg void OnSize() {}
};
使用这个简单的代码:
void CRestoreSettingsDlg::OnSize(UINT nType, int cx, int cy)
{
CResizingDialog::OnSize(nType, cx, cy);
m_gridBackupLog.ExpandLastColumn();
}
为什么它被标记了?
C26434 Function
'CRestoreSettingsDlg::OnSize'
hides a non-virtual function'CRestoreDialogDlg::OnSize'
.
如你所见,我调用了基础class方法。
声明和定义
CRestoreSettingsDlg
:
public:
afx_msg void OnSize(UINT nType, int cx, int cy);
void CRestoreSettingsDlg::OnSize(UINT nType, int cx, int cy)
{
CResizingDialog::OnSize(nType, cx, cy);
m_gridBackupLog.ExpandLastColumn();
}
CResizingDialog
:
public:
afx_msg void OnSize(UINT nType, int cx, int cy);
void CResizingDialog::OnSize(UINT nType, int cx, int cy)
{
CDialogEx::OnSize(nType, cx, cy);
Invalidate(TRUE);
}
- 样板基础 class (
afxwin.h
) 似乎有:
protected:
afx_msg void OnSize(UINT nType, int cx, int cy);
_AFXWIN_INLINE void CWnd::OnSize(UINT, int, int)
{ Default(); }
继承
class CRestoreSettingsDlg : public CResizingDialog
class CResizingDialog : public CDialogEx
C26434 warning documentation links to C.128 C++ Core Guidelines Rule。它解释了要强制正确使用虚函数,非虚函数隐藏应该产生警告。
但是,对于 MFC 消息映射,您必须按照宏中指定的方式命名消息处理程序,在本例中为 OnSize
,并且由于消息处理程序已经由虚函数(隐藏在 *_MESSAGE_MAP()
宏),消息处理程序本身不必是虚拟的。
因此它可能被视为误报。或者可能被 MFC 本身视为违反上述 C.128 规则。毫不奇怪 - MFC 比这些准则早几十年。
所以我想您可以继续并为所有 afx_msg
函数抑制它。也许重新定义 afx_msg
以包含 __pragma(warning(suppress(...)))
,或者只是对 afx_msg
块进行抑制。
一些抑制选项(Godbolt's compiler explorer demo):
#define afx_msg // this is normally defined by MFC
struct base
{
afx_msg void OnSize(){}
};
struct derived1 : base
{
afx_msg void OnSize() {} // produces C26434
};
// Suppression by adding some code:
struct derived2 : base
{
#pragma warning(push)
#pragma warning(disable:26434)
afx_msg void OnSize() {}
#pragma warning(pop)
};
struct derived3 : base
{
[[gsl::suppress(c.128)]] afx_msg void OnSize() {}
};
// Suppression by redefining MFC macro -- dirty but less intrusive:
#undef afx_msg
#define afx_msg __pragma(warning(suppress:26434))
struct derived4 : base
{
afx_msg void OnSize() {}
};
#undef afx_msg
#define afx_msg [[gsl::suppress(c.128)]]
struct derived5 : base
{
afx_msg void OnSize() {}
};