Rob*_*Rob 7 c++ operator-overloading
在我的课程中,我经常operator!=通过返回写一个快速!(*this == rhs),例如:
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);
}
};
Run Code Online (Sandbox Code Playgroud)
这样做我看不出任何明显的问题,但我想我会问是否有人知道.