| 46 | // Copy constructor |
| 47 | template <typename T> |
| 48 | Stack<T>::Stack(const Stack& stack) |
| 49 | { |
| 50 | if (stack.m_head) |
| 51 | { |
| 52 | m_head = std::make_unique<Node>(stack.m_head->m_item); // Copy the top node of the original |
| 53 | Node* oldNode {stack.m_head.get()}; // Points to the top node of the original |
| 54 | Node* newNode {m_head.get()}; // Points to the node in the new stack |
| 55 | |
| 56 | while (oldNode = oldNode->m_next.get()) // If m_next was nullptr, the last node was copied |
| 57 | { |
| 58 | newNode->m_next = std::make_unique<Node>(oldNode->m_item); // Duplicate it |
| 59 | newNode = newNode->m_next.get(); // Move to the node just created |
| 60 | } |
| 61 | } |
| 62 | } |
| 63 | |
| 64 | // Copy assignment operator |
| 65 | template <typename T> |