【发布时间】:2017-03-24 20:57:37
【问题描述】:
在 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;
};
【问题讨论】:
-
This question 可能会有所帮助。
标签: c++ operator-overloading binary-operators relational-operators