In my classes I often write a quick operator!= by returning !(*this == rhs), e.g.:
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); } };
I can’t see any obvious problems with doing this but thought I’d ask if anyone knows of any.
I believe that’s the preferred method of implementing
operator!=so that you don’t repeat yourself, and you have a guaranteed correct relationship withoperator==.