From 08dc634ebbd1e5b6682a21fa294ab884f0253c1d Mon Sep 17 00:00:00 2001 From: Simon Bruder Date: Sat, 22 Apr 2023 12:53:03 +0200 Subject: [PATCH] u01: Make destructor work with recursive trees --- u01/node.cpp | 5 ++++- u01/node.h | 3 +++ 2 files changed, 7 insertions(+), 1 deletion(-) diff --git a/u01/node.cpp b/u01/node.cpp index 1209553..0525148 100644 --- a/u01/node.cpp +++ b/u01/node.cpp @@ -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; } diff --git a/u01/node.h b/u01/node.h index f991a00..35176bb 100644 --- a/u01/node.h +++ b/u01/node.h @@ -18,6 +18,9 @@ public: private: std::string name; std::vector children; + bool destroy = + false; // gets set by the destructor, + // allows deletion of recursive trees with a recursive destructor static unsigned int node_id; };