Thread: Using == Operator with Classes

  1. #1
    Registered User mikeman118's Avatar
    Join Date
    Aug 2007
    Posts
    183

    Using == Operator with Classes

    Hi,

    Like the tile says, would it be possible to use the == operator with a class? For example:
    Code:
    class Class
    {
        ...
    }
    Class cls1, cls2;
    if (cls1 == cls2)
    {
        ...
    }
    If it's difficult or something complicated then never mind, but if it's not too difficult can anyone tell me how to do this? Thanks.

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    http://www.parashift.com/c++-faq-lit...erloading.html
    Any decent reference material should tell you how to do this.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    Yes. You just have to overload it. Either as a free function:
    Code:
    bool operator ==(const Class &lhs, const Class &rhs)
    {
      return codeThatDeterminesIfLhsAndRhsAreEqual();
    }
    Or as a member function.
    Code:
    bool Class::operator ==(const Class &rhs)
    {
      return codeThatDeterminesIfThisAndRhsAreEqual();
    }
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  4. #4
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    You need to declare an "operator==(const Class &rhs)" in your class - or you can declare an external "operator==(const Class &lhs, const Class &rhs)". [1]

    You then need to "compare" your two elements (*this, rhs or lhs, rhs depending on which you choose). For example:
    Code:
    class C 
    {
    private:
       int x;
       int y;
    ...
    public:
       bool operator==(const C &rhs)
       {
          if (rhs.x == x && rhs.y == y) return true;
          return false;
       };
    };
    Of course, depending on the contents of your class, the comparison may be more or less complex, and you don't necessarily need to compare ALL the fields. If a record is a "person", then "social security number" or similar may be the component that says two persons are the same [obviously, that would have to be checked elsewhere that two different persons aren't entered with the same number]. Or a car-registration number for a car, serial number for a computer, etc.


    [1] lhs stands for Left Hand Side and rhs stands for Right Hand Side, and corresponds to the left and right hand side operands to the == operator.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  5. #5
    Registered User mikeman118's Avatar
    Join Date
    Aug 2007
    Posts
    183
    Thank you everyone, it worked! (That was easy)

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Simulator
    By MasterAchilles in forum C Programming
    Replies: 10
    Last Post: 11-30-2008, 10:31 PM
  2. Tic Tac Toe!
    By Sinensis in forum C Programming
    Replies: 2
    Last Post: 10-21-2008, 04:40 PM
  3. Another syntax error
    By caldeira in forum C Programming
    Replies: 31
    Last Post: 09-05-2008, 01:01 AM
  4. Homework help
    By mkdl750 in forum C Programming
    Replies: 45
    Last Post: 07-17-2008, 09:44 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