Why does the Node pointer in the Node class need template arguments (such as linked lists)?

In the Node framework, I don’t know why I should use Node * forward and not Node<T> * forward . The same question applies to Node * backward . At first I thought it was because you never need to look forward / back inside your nodes ... they are just direct node pointers, and they don’t care what is there. But then I found that I got to nodes with calls like T temp = last->backward->data , and everything worked fine. Therefore, my theory comes out of the window.

 template <typename T> struct Node { public: T data; Node<T> * forward { nullptr }; //<-This works...but Node * backward { nullptr }; //<-I've been told no <T> is right }; template <typename T> class DoublyLinkedList{ public: /* ...methods... */ private: unsigned int size {0}; Node<T> * first {nullptr}; Node<T> * last {nullptr}; }; 

Any comments or suggestions on how I can better understand this would be greatly appreciated. Thanks.

+5
source share

Source: https://habr.com/ru/post/1257677/


All Articles