我想實例化一個對象並在之後刪除它,我做錯了什麼?實例化並刪除一個對象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();方法刪除它的對象?
@SteJ這就是沒有任何錯誤的地步,我的問題是,如何在刪除對象後成功調用showError方法? –
我發現了評論,對不起。我的答案如下。 – SteJ
如果成員函數'showError'從不使用'ErrorHandler'中的成員數據,那麼'this'將從解除分配的'errPtr'中被取消引用。這仍然是未定義的行爲,但是因爲你的糟糕的'this'永遠不會被解除引用,所以這種非靜態成員函數更像靜態。 –