#Do you need to write a destructor for a Node class used in linkedlists?
26 messages · Page 1 of 1 (latest)
When your question is answered use !solved to mark the question as resolved.
Remember to ask specific questions, provide necessary details, and reduce your question to its simplest form. For tips on how to ask a good question run !howto ask.
LinkedList::~LinkedList() {
Node* currentNode = head_;
Node* nextNode;
while (currentNode != nullptr) {
nextNode = currentNode->getNextNode();
delete currentNode;
currentNode = nextNode;
}
head_ = nullptr;
tail_ = nullptr;
}
Node::~Node() {
delete this;
}
class Node {
public:
Node(std::string &name, double value);
~Node();
// Getters
Node* getNextNode();
std::string getName();
double getValue();
// Setters
void setNextNode(Node* nextNode);
void setName(std::string &name);
void setValue(double value);
private:
Node* nextNode_;
std::string name_;
double value_;
};
this is bad
think about what it does
in your case, I think just writing ~Node() = default; in the header is enough
It just deallocates the memory in which itself is stored at, and I have no idea what that does; I think it works like that

I googled that delete calls the destructor and assumed everything needed its own destructor, but I don't know what I should write in there
Do you mind explaining why it's bad?
I'm very new to memory allocations
nothing, in your case the default compiler generated destructor is fine
.
because you're calling delete this in its own destructor... but delete calls the destructor... but then the destructor does delete this which then calls the destructor... but then the destructor does delete this which calls the destructor...
I seeee... circular
would get a stackoverflow?
probably
@delicate prism Has your question been resolved? If so, run !solved :)
!solved
Thank you and let us know if you have any more questions!