c++ - 实例化和删除对象 C++

标签 c++

我正在尝试实例化一个对象并在之后将其删除,我做错了什么?

default:
        //Instantiate object of the ErrorHandler class
        errPtr = new ErrorHandler();
        //Print the error message to the console screen
        errPtr->showError("Invalid input, please select option 1 or 2, and press enter.");
        delete errPtr;
        errPtr->showError("hello"); //This line is being executed normally like object still exist.
        break;

我的问题是我怎么可以调用 showError();删除它的对象后的方法?

最佳答案

errPtr->showError("hello"); //This line is being executed normally like object still exist.

不过这只是未定义的行为。

如果您取消引用之前被deleted 的指针,任何事情都可能发生,包括看似正确的行为。

关于c++ - 实例化和删除对象 C++,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36391299/

相关文章:

c++ - 预先计算数据与顺序处理

c++ - 从 boost::array 更改为 std::array 时出现错误

c++ - Qt 错误 : Searching for a function that does not exist

c++ - 如何将 sqlite3 + 扩展函数静态链接到 C/C++ 应用程序中?

c++ - BMP 文件格式无法正确读取?

c++ - 渲染一个透明的 QLabel

c++ - 如何在编译时找出 integer_sequence 是否包含给定的数字?

c++ - 如何用触摸数据模拟鼠标点击?

c# - 接口(interface)的用途和继承方面的区别

c++ - 为什么这不是函数模板的部分特化?