u01: Make destructor work with recursive trees

This commit is contained in:
Simon Bruder 2023-04-22 12:53:03 +02:00
parent 5ac5bd1f43
commit 08dc634ebb
2 changed files with 7 additions and 1 deletions

View file

@ -16,8 +16,11 @@ node::node(const std::string &name) : name(name) {
node::~node() {
std::cout << "enter ~node() of \"" << get_name() << "\"" << std::endl;
this->destroy = true;
for (node *child : children) {
delete child;
if (!child->destroy) {
delete child;
}
}
std::cout << "leave ~node() of \"" << get_name() << "\"" << std::endl;
}

View file

@ -18,6 +18,9 @@ public:
private:
std::string name;
std::vector<node *> children;
bool destroy =
false; // gets set by the destructor,
// allows deletion of recursive trees with a recursive destructor
static unsigned int node_id;
};