0

I am new to overloading operators. I am trying to overload a bool operator. I am currently using the bool operator as an access function into the Date class. Any suggestions how I would go about converting the bool EqualTo function to overload the operator? Thank you!

class Date {
private:
    int mn;        //month component of a date
    int dy;        //day component of a date
    int yr;        //year comonent of a date

public:
    //constructors
    Date() : mn(0), dy(0), yr(0)
    {}
    Date(int m, int d, int y) : mn(m), dy(d), yr(y)
    {}

    //access functions

    int getDay() const
    {
        return dy;
    }
    int getMonth() const
    {
        return mn;
    }
    int getYear() const
    {
        return yr;
    }

    bool EqualTo(Date d) const;

};

bool Date::EqualTo(Date d) const
{
    return (mn == d.mn) && (dy == d.dy) && (yr == d.yr);
}
larry718
  • 13
  • 8
  • Change the declaration to `operator bool() const`. But you don't want that. You want to create `friend operator==(const Date& lhs, const Date& rhs)`. – sweenish May 06 '20 at 17:18
  • 2
    Looks more like you are trying to overload `operator==` to compare two objects? – Lukas-T May 06 '20 at 17:19
  • 1
    Why are you trying to overload `operator bool()`? You seem to be comparing 2 `Date`s, so you should probably overload `operator==`. – cigien May 06 '20 at 17:19
  • I should have mentioned that it will be an array of Dates. – larry718 May 06 '20 at 17:21
  • I'm not sure what that has to do with checking if 2 dates are equal? – cigien May 06 '20 at 17:22
  • @larry718 That doesn't change the fact that you want the equality operator and not the bool conversion. – sweenish May 06 '20 at 17:22
  • There is a quite extensive answer about operator overloading [here](https://stackoverflow.com/questions/4421706/what-are-the-basic-rules-and-idioms-for-operator-overloading). – Lukas-T May 06 '20 at 17:26

1 Answers1

5

The implementation of your EqualTo function suggests that you should be overloading operator== to test if 2 Date objects are equal. All you have to do is rename EqualTo to operator==. And you should take the arguments by const&.

bool Date::operator==(Date const &d) const
{
    return (mn == d.mn) && (dy == d.dy) && (yr == d.yr);
}

Inside the class Date, the declaration would look like:

bool operator==(Date const &d) const;

Another way to do this is to make the operator a friend of the class:

friend bool operator==(Date const &a, Date const &b) const
{
    return (a.mn == b.mn) && (a.dy == b.dy) && (a.yr == b.yr);
}

Note that in this case, this is not a member function. In this case, you can define it inside the class (where you need the friend keyword).

If you define a friend function outside the class, you still need to declare it as a friend within the class. However, the definition can no longer have the friend keyword.

I would also recommend naming your variables a bit more clearly, such as month, day, and year.

cigien
  • 57,834
  • 11
  • 73
  • 112
  • Great! How would I declare in Date? – larry718 May 06 '20 at 17:26
  • Added more explanation. – cigien May 06 '20 at 17:29
  • I implemented the first example into my code. Now I am getting an error - "Out-of-line definition of 'operator==' does not match any declaration in 'Date'" – larry718 May 06 '20 at 17:30
  • @cigen the declaration is usually marked as friend ;) Its worth mentioning that these should be public member functions, and that const can be placed on either side ;) – Waqar May 06 '20 at 17:34
  • @Waqar Yes, it's in the answer. Is it not clear enough? Edited a bit. – cigien May 06 '20 at 17:35
  • @cigien its clear. Just one more thing I think, if you declare the friend function inside the class, then definition outside the class cannot have `friend` in its signature. – Waqar May 06 '20 at 17:40