<分区>
我写这个程序只是为了检查如果我们在类的析构函数中删除这个指针会发生什么。这是我的代码
class xxx{
public: xxx(){cout<<"constructor called"<<endl;}
~xxx(){cout<<"destructor called"<<endl;delete(this);} //deleting the 'this' pointer
};
int main(int argc, char *argv[])
{
xxx *x1=new xxx();
delete x1;
return 0;
}
当我运行这个程序时,它会导致无限循环打印“desctructor called”。
delete
运算符和析构函数之间有什么关系?另外,当您在析构函数中使用 delete this
时会发生什么情况?