Question: I am working with stacks and need to check to see if two are the same. I have overloaded the 

```
bool operator== 

```

function in my code, and now I need to put the logic in the code. I will check a few things to see if the two stacks are the same: the length, the data type, and the content of each element. Length and content are no problem, its the data type that is giving me issues.

I tried to make a function:

```
...
    Type getType();
};

template <class Type>
Type getType(){ returnType;}

```

But this did not work.

I also thought about:

```
bool operator== (stack<Type> &lhs, stack<Type> &rhs){

    return (lsh.Type == rhs.Type);
//additional conditions will be checked.
}

```

How to I check if they are the same type?

**EDIT:** What if I just checked the data type of the top elements of the two stacks? would that be sufficient?

Answer: 
If you implement the stack data structure using *templates* (e.g. `template <typename T> class Stack...`), then you just need to overload `operator==` and check sizes and do an item-wise comparison:

```
template <typename T>
bool operator==(const Stack<T> & lhs, const Stack<T> & rhs)
{
  if (lhs.Size() != rhs.Size())
    return false;

  // Compare item by item
  ...
}

```

The type will be implicitly checked by the C++ compiler type system (if you have a `Stack<int>` and try to compare against a `Stack<string>` you will have a compiler error).
I found a simple program which will solve your purpose, Its simple and very efficient way of checking if two stacks are equal or not. 

```
  bool isEqual(stack<Type> &stack1, stack<Type> &stack2) {
    if (stack1.empty() && stack2.empty())
      return true;
    else if (stack1.empty() || stack2.empty())
      return false;
    while (!stack1.empty() && !stack2.empty())
    {
      if (stack1.top() != stack2.top())
        return false;
      stack1.pop();
      stack2.pop();
    }
    if (stack1.empty() && stack2.empty())
      return true;
    else
      return false;
  }

```