My question is pretty simple I just want to know how to call
My question is pretty simple, I just want to know how to call my operator== function in Stack.cpp using a list function.
Here is what I meant. This is my Stack.h file:
class Stack {
public:
/**constructors and destructors*/
Stack();
Stack(const Stack &S);
~Stack();
void pop();
void push(int data);
bool operator==(const Stack &S);
[ .......]
private:
List<int> stack;
};
Here is my List.h file:
template<class listitem>
class List {
private:
struct Node {
listitem data;
Node* next;
Node* previous;
Node(listitem data) :
next(NULL), previous(NULL), data(data) {
}
};
typedef struct Node* NodePtr;
NodePtr start;
NodePtr end;
NodePtr cursor;
int length;
public:
........
bool operator==(const List &list);
[.......] }
[........]
template<class listitem>
bool List<listitem>::operator==(const List& list) {
if (length != list.length)
return false;
NodePtr temp1 = start;
NodePtr temp2 = list.start;
while (temp1 != NULL) {
if (temp1->data != temp2->data)
return false;
temp1 = temp1->next;
temp2 = temp2->next;
}
return true;
}
[......]
So I already define my functions in List.h and I only need to call my function from List again in my Stack.cpp file. For example from my Stack.cpp:
Stack::Stack(const Stack &S) :
stack(S.stack) {
}
void Stack::pop() {
stack.remove_start();
}
[......]
And I don\'t know how to call my operator== function in Stack.cpp from the List.h like the way above. This is what my guess so far but did not work:
bool Stack::operator==(const Stack &S) {
stack.operator==(S.stack);
}
Thank you
Solution
When we wish to make a comparison, such as in an if statement, we use the double equals sign (==).
1)bool operator==(const Stack &S); [ .......]
It means whatever the boolean operator it should be belongs to stack or equal to.
2) bool operator==(const List &list);
Likewise link list also compare with the constructor link list.
and constructor need not to call explicitly. it will call itself when program called first.

