#include<iostream>
#include<conio.h>
using namespace std;
class Marie{
public:
int x;
Marie(){
cout<<"created";
}
~Marie(){
cout<<"hii i am destructor";
}
void ShowMarie() {
cout<<"friends";
x=5;
delete this; /*<--- here destructor called */
}
};
int main(){
Marie *ptr = new Marie;
ptr->ShowMarie();
cout<<ptr->x; /*<---- ptr is dangling pointer but it is still showing me the correct value.*/
getch();
}
- 调用对象的析构函数后,它仍然像在内存中一样引用吗?为什么?
- 为什么我们需要为动态创建的对象显式调用析构函数
delete this
呢?
- 如果我们使用
delete this;
内部析构函数会发生什么?这是递归调用析构函数吗?