u01: Make destructor work with recursive trees
This commit is contained in:
parent
5ac5bd1f43
commit
08dc634ebb
|
@ -16,9 +16,12 @@ node::node(const std::string &name) : name(name) {
|
||||||
|
|
||||||
node::~node() {
|
node::~node() {
|
||||||
std::cout << "enter ~node() of \"" << get_name() << "\"" << std::endl;
|
std::cout << "enter ~node() of \"" << get_name() << "\"" << std::endl;
|
||||||
|
this->destroy = true;
|
||||||
for (node *child : children) {
|
for (node *child : children) {
|
||||||
|
if (!child->destroy) {
|
||||||
delete child;
|
delete child;
|
||||||
}
|
}
|
||||||
|
}
|
||||||
std::cout << "leave ~node() of \"" << get_name() << "\"" << std::endl;
|
std::cout << "leave ~node() of \"" << get_name() << "\"" << std::endl;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -18,6 +18,9 @@ public:
|
||||||
private:
|
private:
|
||||||
std::string name;
|
std::string name;
|
||||||
std::vector<node *> children;
|
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;
|
static unsigned int node_id;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
Reference in a new issue