A static check tool shows a violation on the below code:
class CSplitFrame : public CFrameWnd
...
class CVsApp : public CWinApp
CWnd* CVsApp::GetSheetView(LPCSTR WindowText)
{
CWnd* pWnd = reinterpret_cast<CSplitFrame*>(m_pMainWnd)->m_OutputBar.GetChildWnd(WindowText);
return pWnd;
}
ERROR MSG:
Class 'CSplitFrame' inherits from class 'CWnd'
DESCRIPTION Avoid casts down the inheritance hierarchy. This rule detects casts from a base class pointer to a subclass pointer.
BENEFITS Allowing casts down the inheritance hierarchy leads to maintenance problems, and downcasting from a base class is always illegal.
Reference:
1. Scott Meyers, "Effective C++: 50 Specific Ways to Improve
Your Programs and Design", Second Edition, Addison-Wesley,
(C) 2005 Pearson Education, Inc.,
Chapter: "Inheritance and Object-Oriented Design", Item 39
2. JOINT STRIKE FIGHTER, AIR VEHICLE, C++ CODING STANDARDS
Chapter 4.23 Type Conversions, AV Rule 178
Do you think it's a good practice for not casting down from a base class pointer to a subclass pointer? Why and When should I follow this rule?
Thanks in advance.