#include <iostream>
#include<stdio.h>
using namespace std;
class aaa{
public:
void methodTest(){
cout << "line1\n";
cout << "line2\n";
cout << "line3\n";
delete this;
cout << "line4\n";
cout << "line5\n";
cout << "line6\n";
cout << "line7\n";
cout << "line8\n";
}
virtual ~aaa(){
cout <<"destrcutor aaa\n";
}
};
int main(int argc, char**argv) {
aaa* ptr = new aaa();
cout <<"============first time==============\n";
ptr->methodTest();
cout <<"============second time==============\n";
ptr->methodTest();
return 0;
}
输出
============first time==============
line1
line2
line3
destrcutor aaa
line4
line5
line6
line7
line8
============second time==============
line1
line2
line3
RUN FINISHED; Segmentation fault; core dumped; real time: 70ms; user: 0ms; system: 0ms
我的问题是为什么我们可以ptr->methodTest()
第二次运行?ptr->methodTest()
第二次调用时应该立即进行coredump,对吗?为什么它仍然能够运行直到达到“删除这个”