u01: Make destructor work with recursive trees
This commit is contained in:
parent
5ac5bd1f43
commit
08dc634ebb
|
@ -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;
|
||||
}
|
||||
|
|
|
@ -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;
|
||||
};
|
||||
|
||||
|
|
Reference in a new issue