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 `operator==` like this:

```
template<typename Type>
bool operator== (const stack<Type> &lhs, const stack<Type> &rhs){
    // compare and return.
}

```

You already know that both stacks will contain elements of the same type. There's no need to check for that.

If you want to allow stacks using different template arguments to be compared, you could do something like this:

```
template<typename Type1, typename Type2>
bool operator== (const stack<Type1> &lhs, const stack<Type2> &rhs){
    // compare
}

```

Then compare the elements using `operator==` and you'll be done. Of course, if there's no `operator==` for parameters of type `Type1` and `Type2`, then the compiler will issue an error.

*Edit*: Since you want a nice error message, you could do this(using C++11):

```
#include <type_traits>

template<typename Type1, typename Type2>
bool operator== (const stack<Type1> &lhs, const stack<Type2> &rhs){
    // This is a compile time assert!
    static_assert(std::is_same<Type1, Type2>::value, "Types are not equal!");
    // compare
}

```

I'd avoid this anyway. It's simpler to let the compiler issue its own error, rather than doing this check yourself.
As you can see in C++ tag definition of stackoverflow

```
C++ is a widely-used, statically-typed, free-form, compiled,
multi-paradigm, multi-level, imperative, general-purpose,
object-oriented programming language.

```

In this definition `statically-typed` mean all type information must be clean in compile time so in C++ if you want to have container of different types or you want to compare containers of different types, you must identify it in compile time through use of templates.

So when you are comparing 2 different type of containers (here stack) you know type of both containers and you do not need to compare their types. So if you want containers of different types, they should all derive from same class so they can stored as same type or you must hold them as `void*` (this is very dangerous unless you know what you are doing). in case that you derive all classes from same class, you can simply make base class polymorphic (by adding at least one virtual function) and use typeid to get type of object or you can add a virtual function that do comparison and then specialize it in each derived class (or even you can mix both techniques) but in case of `void*` you must have another way to identify type of objects!