c++ - 为什么 “delete *this” 会编译?

标签 c++ compilation mfc

我正在从 MFC CDialogEx 派生一个类:

class MyDialog : public CDialogEx
{
  public:
    virtual void PostNcDestroy();
    …
  …
};

我实现了PostNcDestroy as such :

void MyDialog::PostNcDestroy()
{
    CDialogEx::PostNcDestroy();
    delete *this; // oops, typo
}

我很惊讶地看到这段代码编译(使用 VC120 或 Visual Studio 2013),并且根本没有生成任何警告。谁能告诉我为什么会这样?

谢谢。

最佳答案

这是一个隐式转换; CWnd类有一个operator HWND()转换函数,HWND是一个指针类型。

删除 HWND 是一个错误,但编译器不知道,也无法警告您。

关于c++ - 为什么 “delete *this” 会编译?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36692605/

相关文章:

c++ - 对于可重定位的多平台安装,理想的 cmake 安装目录结构是什么?

c++ - (共享)指向单例的指针

python - 使用 pythonbrew 编译 Python 3.2 和 2.7 时出现问题

c++ - 如何确保在 C++ 编译期间将参数视为常量?

windows - 如何使用MFC统计Windows中每个进程的线程数

c++ - 什么时候关闭 CAsyncSocket 是安全的?

c++ - 即使 V(U) 有效,从 std::pair<T, U> 到 std::pair<T, V> 的转换也不起作用?

c - 为什么 GCC 会产生以下 asm 输出?

c++ - Direct2D - 截图

c++ - std::move 意外调用析构函数