Proper way of overloading binary relational operators in C++ -
this question has answer here:
what proper/canonical way of overloading binary relational operators in c++?
is better use member functions, or friend
free functions?
e.g.:
class x { public: ... // use member function overloads bool operator==(const x& rhs) const { return m_text == rhs.m_text; } private: std::string m_text; };
or:
class x { public: ... // use friend free function overloads friend bool operator==(const x& lhs, const x& rhs) { return lhs.m_text == rhs.m_text; } private: std::string m_text; };
the 1 thing should aware of implicit conversions.
if class supports implicit conversions other types, may useful make operator== friend support implicit conversions on first arguments.
in other cases suppose more matter of style.
Comments
Post a Comment