Not always the case example:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34
|
#include <iostream>
#include <string>
#include <type_traits>
class Num {
unsigned long long value;
public:
Num(unsigned long long value) : value(value){}
template <typename T, typename std::enable_if<std::is_convertible<T,
unsigned long long>::value,int>::type = 0>
Num& operator += (const T& other) {
value += other;
return *this;
}
operator unsigned long long(void) const {
return value;
}
};
int main() {
Num t = 10;
int add = 10;
t += add;
Num g = 90;
t += g;
std::cout << t << std::endl;
return 0;
}
|
As you can see, the overloaded
+= operator is not a friend operator but works for any
Num type and any
integral type. If you recall, the implicit parameter of
const Num* this
is passed in as the first parameter for member operators, so this function does infact take 2 different objects as parameter, but the
this parameter is passed implicitly
So the types of objects does not determine what you can pass in, another example of this is
std::hash<T>::operator()(const T&);
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36
|
#include <iostream>
#include <functional>
#include <string>
struct S
{
std::string first_name;
std::string last_name;
};
namespace std
{
template<>
struct hash<S>
{
typedef S argument_type;
typedef std::size_t result_type;
result_type operator()(argument_type const& s) const
{
result_type const h1 ( std::hash<std::string>()(s.first_name) );
result_type const h2 ( std::hash<std::string>()(s.last_name) );
return h1 ^ (h2 << 1);
}
};
}
int main()
{
S s;
s.first_name = "Bender";
s.last_name = "Rodriguez";
std::hash<S> hash_fn;
std::cout << "hash(s) = " << hash_fn(s) << "\n";
}
|
The above taken from
http://en.cppreference.com/w/cpp/utility/hash
Class operators can be overridden to take any type as a parameter, but there can only be one of such type.