class sortedList
{
class Node
{
public:
int value;
Node* next;
Node()
{
this->value = 0;
this->next = NULL;
}
Node(int value)
{
this->value = value;
this->next = NULL;
}
}* head = NULL, * tail = NULL;
public:
void addNode(int value)
{
Node* n = Node(value);
//More code...
}
};
Why does adding new
before Node(value)
make the code correct?
I think the code above should stay wrong because pointer doesn't equal a node.
This is not valid code:
Node* n = Node(value);
The reason new
makes it work is that new
returns a pointer.
You can either declare your Node
using dynamic storage:
Node* n = new Node(value);
or automatic storage:
Node n(value);