If I want to overload the == operator, and I do so by doing something like:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
bool Stack<T>::operator==(const Stack<T>& rhs) const{
for(int i = 0; i < MAX_STACK;i++){
T topRHS,topLHS;
rhs.getTop(topRHS);
this->getTop(topLHS);
//rhs.pop(topRHS); // why can't I do this?
//this->pop(topLHS); // why can't I do this either?
if(topLHS != topRHS){
returnfalse;
}
}
I want to use the == operator to check if my Stacks are the equal. Stacks are equal if every element in the stack are exactly the same. So, am I thinking about it correctly by using a for loop to check if each stack element on the left hand side of == is the same as the right hand side? Whenever I try to pop the compiler doesn't let me enter the function, it just sends me to the disassembly (i'm not sure what that is either).
Strange that it allows you to compile. My guess was going to be: Your program cannot compile because most likely pop() is non-const while operator== is const. But you say it shows assembly code, which leads me to think that you were in fact able to compile.
It is principally incorrect approach. You cannot compare a stack by popping its elements. Because after this operation you get an ampty or modified stack that does not correspond to the original. It is supposed that after comparision compared objects will not be changed and wiil keep their original state.
If your stack is an adapter of another container then usually such an operation with stacks is realized by comparing base containers which have already such operation defined.
Yes, I see. But this approach is uneffective. As I said it is better to compare containers which are bases for stacks that is without pop and top operations. For example if a stack is build with an array it is better to compare directly arrays.