I'm trying to use the null object pattern in a doubly linked list in c++, but I can't seem to find a way to use it while keeping the code clean.
The problem lies in the following piece of code:
node->getNext()->setPrevious(node->getPrevious());
node->getPrevious()->setNext(node->getNext());
getNext and getPrevious will return a NullNode object if the next/previous node is a nullptr.
A use case where this fails - when node is the head node:
The following will set the 2nd node's previous pointer to a NullNode object causing a memory leak .
node->getNext()->setPrevious(node->getPrevious());
What I'm trying to accomplish here is to keep the NOP and keep the code clean from nullptr and class type comparison,
Any suggestions will be very appreciated!
ifcheck? E.g.if (node != nullptr) { ... }? - Some programmer dude