Overloading operator ==

This is a discussion on Overloading operator == within the C++ Programming forums, part of the General Programming Boards category; Hi, to practice overloading operators I'm building a Fractions class. I have a question about comparison operators. In a tutorial, ...

  1. #1
    The larch
    Join Date
    May 2006
    Posts
    3,573

    Overloading operator ==

    Hi,
    to practice overloading operators I'm building a Fractions class.
    I have a question about comparison operators. In a tutorial, the overloaded == returned an int (1 or 0) - wouldn't it make more sense to return a bool (true or false)? Would it matter - I guess overloading these operators is important if you want to use the sort algorithm on them and such?

    This is what I have to compare fractions. Is it OK?

    Code:
    class Fraction
    {
        public:
            Fraction(int up = 0, int low = 1):upper(up), lower(low) {}
            ~Fraction() {}
            //....
            bool operator== (const Fraction f)
            {
                if (this->upper * f.lower == this->lower*f.upper)
                    return true;
                else
                    return false;
            }
        private:
            int upper, lower;
    };

  2. #2
    Registered User whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    7,667
    The only thing I have to say, is pass a const reference to Fraction f, so that you don't have to copy Fraction f everytime you use operator ==().

  3. #3
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,799
    Code:
    if (this->upper * f.lower == this->lower*f.upper)
        return true;
    else
        return false;
    Just a nit, but this can be simplified a bit to this:
    Code:
    return this->upper * f.lower == this->lower * f.upper;
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

  4. #4
    The larch
    Join Date
    May 2006
    Posts
    3,573
    Thanks, both are excellent suggestions! Never thought of that...

  5. #5
    Registered User
    Join Date
    Jan 2005
    Posts
    7,317
    >> wouldn't it make more sense to return a bool (true or false)?
    Yes. You should return a bool.

    >> This is what I have to compare fractions. Is it OK?
    If you make it a member function, you should make the function const as well as making the parameter a const reference as citizen mentioned. However, in this case it should not be a member. It should be a non-member function:
    Code:
    bool operator==(const Fraction& f1, const Fraction& f2);
    You would have to make it a friend in this case, since you use the private upper and lower members, but that is ok.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Overloading operators
    By ugmusicbiz in forum C++ Programming
    Replies: 2
    Last Post: 02-13-2009, 12:41 PM
  2. unary operator overloading and classes
    By coletek in forum C++ Programming
    Replies: 9
    Last Post: 01-10-2009, 01:14 AM
  3. overloading operator problems
    By almich in forum C++ Programming
    Replies: 2
    Last Post: 07-26-2004, 04:10 PM
  4. operator overloading
    By blue_gene in forum C++ Programming
    Replies: 6
    Last Post: 04-29-2004, 04:06 PM
  5. overloading
    By theLukerBoy in forum C++ Programming
    Replies: 6
    Last Post: 11-04-2002, 07:49 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21