MCPcopy Create free account
hub / github.com/Apress/beginning-cpp20 / Stack

Method Stack

Examples/NoModules/Chapter 17/Ex17_04B/Stack.h:48–62  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

46// Copy constructor
47template <typename T>
48Stack<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
65template <typename T>

Callers

nothing calls this directly

Calls 1

getMethod · 0.80

Tested by

no test coverage detected