Thread: Overloading ==

  1. #1
    Cheesy Poofs! PJYelton's Avatar
    Join Date
    Sep 2002
    Location
    Boulder
    Posts
    1,728

    Overloading ==

    This is driving me nuts and I'm here at work without my books to help out. For some reason I thought that one didn't need to overload the "==" operator when comparing two objects of the same class but I get an error saying that it isn't defined with left hand operator of myclass. So I tried overloading the operator in the class but I must be doing something wrong because it just becomes circular. Here is what I have:
    Code:
    bool board::operator ==(board& b)
    {
    	return (*this==b);
    }
    I have a feeling its something real easy to fix but for the life of me I can't figure it out.
    Last edited by PJYelton; 01-02-2003 at 01:22 PM.

  2. #2
    Skunkmeister Stoned_Coder's Avatar
    Join Date
    Aug 2001
    Posts
    2,572
    how about return (privatemember1==b.privatemember1)&&(privatemember 2==b.privatemember2)&& so on;
    Free the weed!! Class B to class C is not good enough!!
    And the FAQ is here :- http://faq.cprogramming.com/cgi-bin/smartfaq.cgi

  3. #3
    Veni Vidi Vice
    Join Date
    Aug 2001
    Posts
    343
    Shouldn´t you compare the datamembers and not objects here like

    Code:
    bool board::operator ==(board& b)
    {
    if (datamember == b.datamember)//All conditions
       return true;
    return false;
    }

  4. #4
    Cheesy Poofs! PJYelton's Avatar
    Join Date
    Sep 2002
    Location
    Boulder
    Posts
    1,728
    Thanks, but isn't there a better way? I really don't want to create functions that return each and every private member of the class.

  5. #5
    Skunkmeister Stoned_Coder's Avatar
    Join Date
    Aug 2001
    Posts
    2,572
    You dont have to as members of the class ie bool operator ==(const class&)const has direct access to all private members so its just a case of ensuring that all data members are equal then returning true else return false. The logical && way is best as it offers short circuit evaluation returning false as soon as a condition is not met. So its reasonably efficient.
    Free the weed!! Class B to class C is not good enough!!
    And the FAQ is here :- http://faq.cprogramming.com/cgi-bin/smartfaq.cgi

  6. #6
    Cheesy Poofs! PJYelton's Avatar
    Join Date
    Sep 2002
    Location
    Boulder
    Posts
    1,728
    Learn something new every day. I knew it could access the members within *this but didn't know one could access the second objects datamembers directly as well.

    I need to look in my books tonight because I could have sworn I've read of another way, but this is just fine as well. Thanks!

  7. #7
    Just a Member ammar's Avatar
    Join Date
    Jun 2002
    Posts
    953
    When you are overloading the == operator, you should use the == operator, to compare the data members of the class( which are built-in types ), and ofcourse return a bool, for example:
    Code:
    class myClass {
    public:
         bool operator==( myClass &) const;
         //more members
    private:
         int value1;
         int value2;
    };
    
    //definition of the operator==
    bool myClass::operator==(myClass &right ) const{
    {
         if ( value1 == right.value1 && value2 == right.value2 )
              return true;
         return false;
    }
    Hope it's clear now...
    none...

  8. #8
    Registered User
    Join Date
    Nov 2002
    Posts
    1,109
    I'm not sure, and I'm still learning, but can you do something like this?

    Code:
    bool myClass::operator== (myClass & right) const {
    	if(*this == right)
    		return true;
    	else
    		return false;
    }

  9. #9
    Cheesy Poofs! PJYelton's Avatar
    Join Date
    Sep 2002
    Location
    Boulder
    Posts
    1,728
    >>I'm not sure, and I'm still learning, but can you do something like this?<<

    No, that was what I was doing wrong. You basically end up with an unintentional recursive function that keeps calling itself over and over again because it keeps seeing the '=='!

  10. #10
    Registered User
    Join Date
    Nov 2002
    Posts
    1,109
    okay. thanks.

  11. #11
    Just a Member ammar's Avatar
    Join Date
    Jun 2002
    Posts
    953
    Originally posted by PJYelton
    >>I'm not sure, and I'm still learning, but can you do something like this?<<

    No, that was what I was doing wrong. You basically end up with an unintentional recursive function that keeps calling itself over and over again because it keeps seeing the '=='!
    That's the whole idea, that's why I said in my post:
    you should use the == operator, to compare the data members of the class( which are built-in types ),
    none...

  12. #12
    Senior Member joshdick's Avatar
    Join Date
    Nov 2002
    Location
    Phildelphia, PA
    Posts
    1,146
    Shouldn't the == operator accept two arguments and not one? I've always seen == overloaded like this:
    Code:
    bool operator == (const myClass &lhs, const myClass &rhs)
    {
    	if(lhs.datamember1 == rhs.datamember1 && lhs.datamember2 == rhs.datamember3)
    		return true;
    	return false;
    }
    How should == be overloaded and why should it be done that way?
    FAQ

    "The computer programmer is a creator of universes for which he alone is responsible. Universes of virtually unlimited complexity can be created in the form of computer programs." -- Joseph Weizenbaum.

    "If you cannot grok the overall structure of a program while taking a shower, you are not ready to code it." -- Richard Pattis.

  13. #13
    Just a Member ammar's Avatar
    Join Date
    Jun 2002
    Posts
    953
    Originally posted by joshdick
    Shouldn't the == operator accept two arguments and not one? I've always seen == overloaded like this:
    Code:
    bool operator == (const myClass &lhs, const myClass &rhs)
    {
    	if(lhs.datamember1 == rhs.datamember1 && lhs.datamember2 == rhs.datamember3)
    		return true;
    	return false;
    }
    How should == be overloaded and why should it be done that way?
    The overloaded == function should be a member function, because the left most operand is an object if the class you are overloading the == operator for it.

    The call the compiler will generate is like this:
    if you have:
    Code:
    object1 == object2;
    //the compiler will generate the following:
    object1.operator==(object2);
    So it should be implemented as a member function...
    The code you wrote implements it as a friend, and I'm not sure if it is going to work, because I didn't test it, but even if it does, you should not use it, because whenever you can use a member function you should not use a firend...

    Note:
    If you didn't get it, just tell me, I'll be glad to help...
    Last edited by ammar; 01-03-2003 at 08:02 AM.
    none...

  14. #14
    the hat of redundancy hat nvoigt's Avatar
    Join Date
    Aug 2001
    Location
    Hannover, Germany
    Posts
    3,130
    Code:
    bool SomeClass::operator == ( const SomeClass& other ) const
    {
    	if( this->Data == other.Data ) 
    	{
    		return true;
    	}
    	else
    	{
    		return false;
    	}
    }
    Note the consts. Data is a primitive member of SomeClass. Obviously, all members that need to be checked should be included in this statement.
    hth
    -nv

    She was so Blonde, she spent 20 minutes looking at the orange juice can because it said "Concentrate."

    When in doubt, read the FAQ.
    Then ask a smart question.

  15. #15
    Senior Member joshdick's Avatar
    Join Date
    Nov 2002
    Location
    Phildelphia, PA
    Posts
    1,146
    Originally posted by ammar
    The code you wrote implements it as a friend, and I'm not sure if it is going to work, because I didn't test it, but even if it does, you should not use it, because whenever you can use a member function you should not use a firend...

    Note:
    If you didn't get it, just tell me, I'll be glad to help...
    Thanks for the offer of help. My confusion stems from the examples provided in my textbook. In those examples, arithmetic operators (+-*/), bool operators(==, >, etc.), and input/output of classes are done outside of the class as free (non-member) functions I looked through the whole textbook but couldn't find anything about the reserved word friend

    Could you please explain it to me a bit, give a short example, when it should be used, and tell me why a member function is better than a friend function? I would really appreciate your help on this. I'm starting to write some classes on my own, and I don't want to screw them all up. Thanks in advance.
    FAQ

    "The computer programmer is a creator of universes for which he alone is responsible. Universes of virtually unlimited complexity can be created in the form of computer programs." -- Joseph Weizenbaum.

    "If you cannot grok the overall structure of a program while taking a shower, you are not ready to code it." -- Richard Pattis.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Another syntax error
    By caldeira in forum C Programming
    Replies: 31
    Last Post: 09-05-2008, 01:01 AM
  2. Homework help
    By mkdl750 in forum C Programming
    Replies: 45
    Last Post: 07-17-2008, 09:44 PM
  3. Separate long string into multiple arrays
    By cashmerelc in forum C Programming
    Replies: 6
    Last Post: 11-27-2007, 02:57 AM
  4. One quick question...
    By Kross7 in forum C++ Programming
    Replies: 10
    Last Post: 04-13-2007, 09:50 PM
  5. Is there a bug in this part of my algorithm for connect 4?
    By Nutshell in forum Game Programming
    Replies: 8
    Last Post: 04-28-2002, 01:58 AM