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

Method pop

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

Source from the content-addressed store, hash-verified

83// Pop an object off the stack
84template <typename T>
85T Stack<T>::pop()
86{
87 if (isEmpty()) // If it's empty pop() is not valid so throw exception
88 throw std::logic_error {"Stack empty"};
89
90 // See Chapter 18 for std::move()
91 auto next {std::move(m_head->m_next)}; // Save pointer to the next node
92 T item {m_head->m_item}; // Save the T value to return later
93 m_head.reset(); // Delete the current head
94 m_head = std::move(next); // Make head point to the next node
95 return item; // Return the top object
96}
97
98template <typename T>
99bool Stack<T>::isEmpty() const { return m_head == nullptr; }

Callers 1

mainFunction · 0.45

Calls

no outgoing calls

Tested by

no test coverage detected