I have a node class that has a pointer to Node indicating its parent Node
class Node
{
.
.
.
Node *parentNode;
};
I receive a pointer to node in the constructor being the parent Node of the current node being created or a NULL value if it's the first node ( I do other stuff in the constructor as well but they are not relevant here
Node(const Node* parentNode) : parentNode(0) //set parentNode to NULL
{
if(parentNode != NULL) //if the parentNode is NULL we just leave it at that
{
//Otherwise we set this pointer to point to this node
this->parentNode = new parentNode(parentNode->parentNode);
}
}
The thing is when I do that, if I create a destructor that destroys the parentNode pointer my program won't work cause I'll be deleting the same memory space I will use later (for example if I pass the node to a function and it gets copied), my copy constructor looks like this:
Node(const Node& n) : parentNode(0)
{
if(n.parentNode != NULL)
{
this->parentNode = new Node(n.parentNode->parentNode)
}
}
Am I wrong about this ? Should I really NOT use a destructor or should I be using something different and then use a destructor.