【发布时间】:2009-01-12 19:16:16
【问题描述】:
在我的课堂上,我经常通过返回!(*this == rhs) 来写一个快速的operator!=,例如:
class Foo
{
private:
int n_;
std::string str_;
public:
...
bool operator==(const Foo& rhs) const
{
return n_ == rhs.n_ && str_ == rhs.str_;
}
bool operator!=(const Foo& rhs) const
{
return !(*this == rhs);
}
};
我看不出这样做有任何明显的问题,但我想问问是否有人知道。
【问题讨论】:
-
+1 仍然喜欢运算符重载而不是 IsEqual(ObjA,ObjB)。我觉得这很整洁