统一的类调用函数,没有例外.这是怎么回事

Unitialized Class Calling Function, No exception. What is going on here?

本文关键字:怎么回事 调用 函数      更新时间:2023-10-16

这是正常行为吗?我以前从没遇到过这种事。我认为它会导致异常,但为什么这里没有呢?看一看

<>之前 CWindowsApplication::MsgProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam) { static CWindowApplication* pApp = NULL; if (message == WM_NCCREATE) { //// retrieve Window instance from window creation data and associate //pApp = reinterpret_cast(((LPCREATESTRUCT)lParam)->lpCreateParams); //::SetWindowLong(hWnd, GWL_USERDATA, reinterpret_cast(pApp)); //pApp = reinterpret_cast(::GetWindowLong(hWnd, GWL_USERDATA)); } pApp->WndProc(hWnd, message, wParam, lParam); // pApp = NULL, but it still works? I expected a exception of some sort. } 之前

但是,当我将类更改为其他东西时,我得到了我期望的异常。这是怎么回事?在我作为一个充满热情的程序员的10多年里,我从来没有遇到过这样的事情。

只要WndProc不是虚的,从技术上讲,为了进行调用,指针根本不需要解引用。这并不是说当您尝试在WndProc中使用this(包括调用带有隐式this的任何虚函数)时它不会崩溃和燃烧,但是非虚调用根据指针的类型进行,并且不需要触摸虚表(或任何其他实例成员)。

你所做的就是调用未定义的行为。这意味着它可以工作,也可以崩溃,或者编译器想让它做什么就做什么。