How to overload...

How do I overload the == operator.

MANN, I have big problems in overloading operators!!!
Who can give me a different tut that talks of overloading operators and the keyword this.
Last edited on
There are two ways you can implement operator == for your class. You can define it as a class method or as non-member function.

This demonstrates the class method.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
class Complex
{
    int real, imaginary;

public:
    int getReal() const { return real; }
    int getImaginary() const { return imaginary; }

    bool operator==(const Complex &param) const
    {
        return param.getReal() == getReal() && param.getImaginary() == getImaginary();
    }
    //...
};


This demonstrates the non-member function:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
class Complex
{
    int real, imaginary;

public:
    int getReal() const { return real; }
    int getImaginary() const { return imaginary; }
    //...
};

bool operator==(const Complex &lhs, const Complex &rhs)
{
        return lhs.getReal() == rhs.getReal() && lhs.getImaginary() == rhs.getImaginary();
}
Last edited on
closed account (o3hC5Di1)
1
2
3
4
5
6
7
8
9
10
11
12
bool operator==(myclass& a, myclass& b)
{
    return (a.value == b.value);
}

// or within myclass:

bool myclass::operator==(myclass& b)
{
    return (value == b.value);  //the current object is used as the left operand
}


If you let us know what part you're having trouble with understanding someone might be able to give you a more in depth explanation.

All the best,
NwN

closed account (zb0S216C)
1
2
3
4
5
6
7
8
// Through a class:
struct Sample
{
    bool operator == (Sample const &right) const;
};

// Globally:
bool operator == (Sample const &left, Sample const &right);

Wazzak
Thanks... any tutorial?
If I overloaded the == operator, I'd probably expect it to return a boolean.

The keyword this is just a self reference to the class your working with.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
// Example
// I'll make the class public for ease of testing

class Foo
{
public:
   int m_int;
   char m_char;
   Foo(int i, char c):m_int(i), m_char(c){}
   bool operator ==(const Foo &other) const
   {
      if (this->m_int  == other.m_int &&
          this->m_char == other.m_char)
     {
         return true;
     }
     return false;
   }
};

// In main
Foo a(10, 'a');
Foo b(10, 'a');

if (a == b)
{
   cout << "Same" << endl;
}
else
{
   cout << "Not same" << endl;
}


Does this example help at all? This overloaded operator returns true if both the m_int and m_char variables are the same for the two classes. If either or both differ, it'll return false.


Edit: Holy crap. I got ninja'd multiple times while writing that one. ¬_¬
Last edited on
just look at an example, any example, you don't need a tutorial, its just a function in a class with a very specific name and two arguments, the body of which compares variables of one class with the other, if they are all equal return true, otherwise return false.
Topic archived. No new replies allowed.