1

Say I have a class called book:

class Book {
    int i;

    public:
        Book(int ii)
        : i(ii) {
        }

        int ISBN() {
            return i;
        }
};

I want to overload the comparison operator for the Book class, so that I can create a bool function that will compare the member "i" when it comes across book1==book2.

bool is_same() {
    return (book1==book2) ? true : false;
}

How would I go about this? This is the current operator overload function I have, it gives me an "invalid initialization of non-const reference of type 'Book&' from an rvalue of the type 'bool'" error. I currently have my overloaded function inside of the class Book as a public function.

Book& operator==(const Book& b) const {
        return ISBN() == b.ISBN();
    }

I'm relatively new to operator overloading, I have sifted through many answers but none of them resolve my issue. I understand how one could simply do book1==book2, but that would only return true if every single member was of the same value. In this case I have more than just one, but I only want to return true if "i" is the same for both objects.

  • 2
    Everything you could want to know: https://stackoverflow.com/questions/4421706/what-are-the-basic-rules-and-idioms-for-operator-overloading – NathanOliver Aug 09 '17 at 17:51
  • 3
    Btw this `return (book1==book2) ? true : false;` could simply be `return book1 == book2;` – DimChtz Aug 09 '17 at 17:52

1 Answers1

4

You basically have 2 choices:

  1. use a member operator with one argument:

    class Book {
    ...
        bool operator==( const Book &an ) const { return ISDN() == an.ISDN(); }
    };
    
  2. use a non-member operator (and possibly a friend statement) with 2 arguments:

    bool operator==( const Book &b1, const Book &b2 )
    {
        return b1.ISBN() == b2.ISBN();
    }
    

Note that ISDN() should be made const.

Either way, you need to return a bool, not a Book &, which is usually returned by the assignment operator =, not the comparison operator ==.

Remy Lebeau
  • 555,201
  • 31
  • 458
  • 770
Slava
  • 43,454
  • 1
  • 47
  • 90
  • This is exactly what I was looking for –  Aug 09 '17 at 17:57
  • I don't want to create an entirely new question just for one little issue, but if I had an object called book1 would it be possible that I could use the current object that is calling the function as a parameter? So if I had a function called compare(), and I called book1.compare(book2) it would automatically compare book1 to book2 instead of making me pass in two objects. –  Aug 09 '17 at 18:11
  • This is what method does (case 1), you can call it as `book1 == book2` or more explicit `book1.operator==( book2 )` – Slava Aug 09 '17 at 18:13
  • so could that be put into a function called compare() that I call with book1.compare(book2)? –  Aug 09 '17 at 18:17
  • @sS5H I do not quite understand your question, you probably better create another one with code sample explaining what you want to achieve. You definitely can write `compare()` as a method that internally calls `==` either way. For example `bool compare( const &b ) const { return *this == b; }` – Slava Aug 09 '17 at 18:24
  • Yes, that is precisely what I had in mind. Thank you –  Aug 09 '17 at 18:39