Note: This question is about MSVC, not about the C++ Standard!
According to the C++ standard, calling any member function (virtual or not) on nullptr
is undefined behaviour (see here and here). As such I was mildly surprised that when I created a MFC Application with the Visual Studio Wizard to find this code:
void CClassView::AdjustLayout()
{
if (GetSafeHwnd() == nullptr)
{
return;
}
//...
Where GetSafeHwnd
is defined as:
_AFXWIN_INLINE HWND CWnd::GetSafeHwnd() const
{ return this == NULL ? NULL : m_hWnd; }
This makes me wonder what the rules of MSVC actually are. According to this implementation, calling a function on a nullptr
seems to be fine, it's just that this
is then the nullptr
. Is this assumption correct for MSVC? What if the member function is virtual? What if it's pure virtual?