<分区>
在 C++ 中重载二元关系运算符的正确/规范方法是什么?
是使用成员函数好,还是friend
免费函数好?
例如:
class X {
public:
...
// Use member function overloads
bool operator==(const X& rhs) const {
return m_text == rhs.m_text;
}
private:
std::string m_text;
};
或:
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;
};