Hello all
I was studying C++ , ADT Stack , and there are two functions which I confused about.
I wrote the codes above , my question is:
we use & while using bool pop function b/c we change the stack but
why we use & for getTop function ? we just retrieve the top of a stack, we don't change it right ?
What I have tried:
bool pop(StackItemType& stackTop);
// Retrieves and removes the top of a stack
// Precondition: None.
// Postcondition: If the stack is not empty,
// stackTop contains the item that was added
// most recently and the item is removed.
// However, if the stack is empty, deletion
// is impossible and stackTop is unchanged.
----------------------------------------------------
bool getTop(StackItemType& stackTop) const;
// Retrieves the top of a stack.
// Precondition: None.
// Postcondition: If the stack is not empty,
// stackTop contains the item that was added
// most recently. However, if the stack is
// empty, the operation fails and stackTop
// is unchanged. The stack is unchanged.