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

Method Stack

Examples/NoModules/Chapter 17/Ex17_04A/Stack.h:38–52  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

36// Copy constructor
37template <typename T>
38Stack<T>::Stack(const Stack& stack)
39{
40 if (stack.m_head)
41 {
42 m_head = new Node {*stack.m_head}; // Copy the top node of the original
43 Node* oldNode {stack.m_head}; // Points to the top node of the original
44 Node* newNode {m_head}; // Points to the node in the new stack
45
46 while (oldNode = oldNode->m_next) // If m_next was nullptr, the last node was copied
47 {
48 newNode->m_next = new Node{*oldNode}; // Duplicate it
49 newNode = newNode->m_next; // Move to the node just created
50 }
51 }
52}
53
54// Destructor
55template <typename T>

Callers

nothing calls this directly

Calls

no outgoing calls

Tested by

no test coverage detected