我的Binary Search Tree析构函数看起来像这样。
~BSTree()
{
if (this == nullptr || this->left == nullptr && this->right == nullptr)
{
return;
}
this->left->~BSTree();
delete this->left;
this->right->~BSTree();
delete this->right;
}
在调用堆栈大约> = 4之后,我的程序if()
由于Access Voilation Exception而崩溃。
我的领域是只有三个:int key;
,BSTree *left;
和BSTree *right;
似乎this
不是NULL,但无法从内存中读取其字段。我如何检查它们remove this;
是否可以阻止异常?
多种事物:
除非与新的展示位置一起使用,否则请不要自己调用析构函数。
按照C ++标准,this
不能等于nullptr
。如果是这样,您将遇到更严重的问题。
delete
已经自动调用了析构函数。
delete
可以顺利通过nullptr
。
您的析构函数应如下所示:
~BSTree()
{
delete left;
delete right;
// possibly ...
left = right = nullptr;
}
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句