I am now designing my own graph class with adjacency list. I finished most of steps except for the destructor.
This is my Vertex class:
struct Vertex{
public:
Vertex(){m_name="";}
Vertex(string name):m_name(name){}
~Vertex(){
cout << "vertex des" << endl;
for(int i = 0; i < m_edge.size(); i++){
delete m_edge[i];
m_edge[i] = nullptr;
}
}
string m_name;
vector<Edge*> m_edge;
};
This is my Edge class:
struct Edge{
public:
Edge(){m_name="";}
Edge(string name):m_name(name){}
~Edge(){
cout << "Edge des" << endl;
delete m_head;
m_head = nullptr;
delete m_tail;
m_tail = nullptr;
}
string m_name;
Vertex* m_head;
Vertex* m_tail;
};
However, I noticed when destructor is called, both 2 classes actually call each other's destructor so this gives me an infinite loop. Is this design problematic? If not, is there is any way to fix this destructor issue? Thanks!
Aucun commentaire:
Enregistrer un commentaire