2008年6月15日星期日

What can the statement delete this in a destructor be harmful?

[What can the statement delete this in a destructor be harmful?]
delete this, will invoke the destructor, so it will be a recursive call to the destructor
infinitely. So the program hangs here.

but delete this can be used beautifully in destructor like self tree recursive destruction like this



~treeNode()

{

if (this->left) {

delete this->left; // first recursive call to destructor treeNode::~treeNode.

this->left = NULL;

}

if (this->right) {

delete this->right; // second recursive call to destructor treeNode::~treeNode.

this->right = NULL;

}

}

0 意見: