How to delete object itself(a dynamic list) by using a destructor
Supposed the class's design cannot be changed. There are dynamic list object linked together.
My destructor doesn't work ,why? Or we cannot use destructor to do so.
Hope you can help me.
Code:
class List{
public:
List();
~List();
private:
List *left;
List *right;
int data;
};
List::~List() {
if (this == NULL)
return;
else {
if (this->left != NULL) {
delete this->left;
left = 0;
}
if (this->right != NULL) {
delete this->right;
right = NULL;
}
delete this;
}
}