template<class T> class LinkedList { public: struct Node //узел { public: ... private: T _value; Node *_next; Node *_prev; }; class Iterator :public std::iterator<std::bidirectional_iterator_tag, T> { public: ... private: Node *node; ... }; private: Node *_head; Node *_ptr; }; Hello. The question is this. is it legal to allocate memory for _ptr not in the constructor but in the method? thus in memory to release? like this:
template<class T> LinkedList<T>::LinkedList() { _head = new Node[1]; _ptr = nullptr; _head->_value = T(); _head->_next = nullptr; _head->_prev = nullptr; } template<class T> LinkedList<T>::~LinkedList() { delete[]_head; delete[]_ptr; } template<class T> typename LinkedList<T>::Node *LinkedList<T>::getPtr(T value, Node *next, Node* prev) { _ptr = new Node[1]; _ptr->_value = value; _ptr->_next = next; _ptr->_prev = prev; return _ptr; }
Node*withshared_ptr<Node>, for example - Abyx