Linked Stack Forbidding Copies
/* * A small variation on the linked stack, that simply forbids * assignment and copying. */ #ifndef _lnkstack2_h_ #define _lnkstack2_h_ template <typename T> class Stack { public: // Initialize the stack empty. Stack() { m_head = NULL; } // Thou shalt not. Stack(const Stack<T>&) = delete; Stack & operator=(const Stack<T>&) = delete; // This deletes the contents of the stack when the // object is destroyed. ~Stack() { // Delete all the nodes. while(m_head != NULL) { node *zombie = m_head; m_head = m_head->next(); delete zombie; } } // Push the argument item. void push(const T &itm) { m_head = new node(itm, m_head); } // Pop the argument item, and return true, or return false if the // the stack is empty. bool pop(T &itm) { if(m_head == NULL) return false; itm = m_head->data(); node *zombie = m_head; m_head = m_head->next(); delete zombie; return true; } // Tell if empty. bool empty() { return m_head == NULL; } private: // Nodes for the linked list. class node { public: node(const T &d, node *n = NULL) { m_data = d; m_next = n; } const T& data() { return m_data; } node *next() { return m_next; } private: T m_data; node *m_next; }; node * m_head; }; #endif