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

Method pop

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

Source from the content-addressed store, hash-verified

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

Callers 1

mainFunction · 0.45

Calls

no outgoing calls

Tested by

no test coverage detected