Quote:Original post by Decrius
Also, should all equality / inequality / shift operators be defined as friend "symmetric" functions rather then "asymmetric" member functions? Are there exceptions?
Generally, I would say yes. This allows implicit conversions (via constructor or conversion operator) for the left hand argument, just as with the right hand side. Consider:
const char* cstring = "foo";std::string cppstring = "bar";std::string a = cppstring + cstring; // works always, uses std::string::string (const char*)std::string b = cstring + cppstring; // won't work unless operator+ is "symmetric"
Not all operators can be done this way (e.g. assignment operators), so obviously those are out, but it generally can't hurt where it's possible.
EDIT: Also, I should share this cheap trick:
template <typename T>class Vector{public: friend bool operator==(const Vector<T> &v1, const Vector<T> &v2) { ... }};
Also works [lol]