template <typename T>
class Stack {
private:
std::vector<T> elems; // elements
public:
Stack(); // constructor
void push(T const&); // push element
void pop(); // pop element
T top() const; // return top element
};
Why is top() a constant function? I think every stack should have it’s own top element, and so to access it a non-constant top() function. Am I wrong?
const!=static.constmeans that the function doesn’t modify any internal variables and the state of the object.