Avoid infinite recursion in destructor
7
1
As part of an exercise my university has tasked me with, I have written a small Graph implementation, following this header. class Node { private: std::string name; std::vector<Node*> children; public: Node(const std::string& name=""); virtual ~Node(); } When writing code for the destructor ~Node() , I noticed that my implementation fails when the graph contains a cycle. This is my implementation so far, which obviously doesn't work if the graph contains a cycle. Node::~Node() { for (Node* n : children) { delete n; n = NULL; } children.clear(); } I am uncertain as to how I would most elegantly write a destructor that can handle cycles in the graph? Please note that I was specifically tasked ...