我正在尝试实例化一个对象并在之后将其删除,我做错了什么?
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.
不过这只是未定义的行为。
如果您取消引用之前被delete
d 的指针,任何事情都可能发生,包括看似正确的行为。
关于c++ - 实例化和删除对象 C++,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36391299/