Hi All,
I have a function that returns the top value in a stack, but only if the stack is not empty. My question is, what value should I return if the stack is empty? The function needs to return an integer, but any integer I return could conceivably be a value in the stack. Is there something I can return that is of type integer, yet isn't an actual number? Here is my code:
1 2 3 4 5 6 7
int top()
{
if(!isEmpty())
return stack[current_size];
elsereturn ?;
}
top() needs to return int but what about isEmpty(), can't you have it return bool? In that case:
top would still be returning a valid value. I would second Peter87's suggestion, but failing that, document that using top when isEmpty returns a value of true results in undefined behavior.