Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 791439
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T21:50:11+00:00 2026-05-14T21:50:11+00:00

I was working on some code recently and decided to work on my operator

  • 0

I was working on some code recently and decided to work on my operator overloading in c++, because I’ve never really implemented it before. So I overloaded the comparison operators for my matrix class using a compare function that returned 0 if LHS was less than RHS, 1 if LHS was greater than RHS and 2 if they were equal. Then I exploited the properties of logical not in c++ on integers, to get all of my compares in one line:

inline bool Matrix::operator<(Matrix &RHS){
  return ! (compare(*this,RHS));
}
inline bool Matrix::operator>(Matrix &RHS){
  return ! (compare((*this),RHS)-1);
}
inline bool Matrix::operator>=(Matrix &RHS){
  return compare((*this),RHS);
}
inline bool Matrix::operator<=(Matrix &RHS){
  return compare((*this),RHS)-1;
}
inline bool Matrix::operator!=(Matrix &RHS){
  return compare((*this),RHS)-2;
}
inline bool Matrix::operator==(Matrix &RHS){
  return !(compare((*this),RHS)-2);
}

Obviously I should be passing RHS as a const, I’m just probably not going to use this matrix class again and I didn’t feel like writing another function that wasn’t a reference to get the array index values solely for the comparator operation.

As per suggestion here is the code if Compare returns -1 for less, 0 for equal and 1 for positive.

inline bool Matrix::operator<(Matrix &RHS){
  return ! (compare(*this,RHS)+1);
}
inline bool Matrix::operator>(Matrix &RHS){
  return ! (compare((*this),RHS)-1);
}
inline bool Matrix::operator>=(Matrix &RHS){
  return compare((*this),RHS)+1;
}
inline bool Matrix::operator<=(Matrix &RHS){
  return compare((*this),RHS)-1;
}
inline bool Matrix::operator!=(Matrix &RHS){
  return compare((*this),RHS);
}
inline bool Matrix::operator==(Matrix &RHS){
  return !(compare((*this),RHS));
}

I don’t know that this really increases the readability though.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-14T21:50:12+00:00Added an answer on May 14, 2026 at 9:50 pm

    As far as I can see it’s safe, but it does take looking twice for everybody reading the code. Why would you want to do this?

    Anyway, for comparison, all you ever need is < and either == or !=, the rest is canonical and I write it mostly by muscle memory. Also, binary operators treating their operands equally (they leave them alone) should IMO be implemented as non-members. Given this, plus using the sane comparison function (-1, 0, +1) and adding the necessary const, I come to this:

    // doing real work
    inline bool operator<(const Matrix& l, const Matrix &r)
    {
      return -1 == compare(l,r);
    }
    inline bool operator==(const Matrix& l, const Matrix &r)
    {
      return 0 == compare(l,r);
    }
    // canonical
    inline bool operator> (const Matrix& l, const Matrix &r) {return r < l;}
    inline bool operator>=(const Matrix& l, const Matrix &r) {return !(l < r);}
    inline bool operator<=(const Matrix& l, const Matrix &r) {return !(r < l);}
    inline bool operator!=(const Matrix& l, const Matrix &r) {return !(l == r);}
    

    The comparisons might not be as clever as yours, but everyone who’s ever seen strcmp() knows immediately what they do. Note that I even added 0 != compare(...), which is completely unnecessary – for the compiler. For humans IMO it makes it more clear what’s going on than the implicit cast to bool. Plus it emphasizes the symmetry to operator<‘s implementation.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 497k
  • Answers 497k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer No, that won't work. There is also no way to… May 16, 2026 at 12:06 pm
  • Editorial Team
    Editorial Team added an answer Opacity applies to the element, not it's background. You either… May 16, 2026 at 12:06 pm
  • Editorial Team
    Editorial Team added an answer How about using a loop: for (var i = 0;… May 16, 2026 at 12:06 pm

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Related Questions

I've been working with some event handling in Javascript and recently decided to migrate
I was working on some code recently and came across a method that had
I am working on some code that previously was using a cfquery, and is
I changed some code on an android application that was working perfectly fine at
I was working on an abstract class to save on some code for a
The project I work on has recently been switched from a horribly antiquated revision
I've been interested in D for a couple of years now and recently decided
I am using Tortoise SVN. I had checked in a crapload of code recently
Recently, I've started to working on Email2SMS feature in our product. When I joined
I've recently been writing some basic command-line programs (I want to keep my skills

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.