what is difference between Node * next and Node<T> * next?

230 Views Asked by At

I was writing implementation of Linked List

in the bellow code we have Node<T> * next; and Node * next; I think both are doing same as pointing to object of Node class or there is difference between them.

template<typename T>
class Node{
    T data;
    Node<T>* next;
    Node * next;
    Node()
    {
        next = nullptr;
    }
    Node(T val)
    {
        this->data = val;
        next = nullptr;
    }
};
1

There are 1 best solutions below

1
On

There is no difference between Node<T> * next; and Node * next;, if both are declared inside the class body.

As stated here.

In the following cases, the injected-class-name is treated as a template-name of the class template itself:

  • it is followed by <
  • it is used as a template argument that corresponds to a template template parameter
  • it is the final identifier in the elaborated class specifier of a friend class template declaration.

Otherwise, it is treated as a type-name, and is equivalent to the template-name followed by the template-parameters of the class template enclosed in <>.

So the name Node * next is resolved into Node<T>* next;.