Simple example of an equality operator
Suppose we have the following class:
class Box { private: int Length; int Width; public: Box(); ... (lots of things)... };
We would like to have an equality operator for Box. It would look something like this:
Prototype statement (put this in the class definition):
bool operator ==(const Box &) const;
Code:
bool Box::operator ==(const Box & Other) const { return (Length == Other.Length && Width == Other.Width); }
Let's go over this carefully. We use this in the form
if (A == B;
and it is equivalent to:
if (A.operator ==(B));
That is, the current instance is the one on the left.
The argument is a constant reference to a Box, as we do not change the value of B when we write "A == B". It is a reference to avoid an unnecessary copy operation.
Likewise, this is a constant method because we do not change the the value of A when we write "A == B".
The actual work being done is just comparing the data.
The return type is bool.
We do not have a default equality operator, so if we need to test one Box equal to another, we need to write our own.
Slightly more complicated example
Suppose our Box also has a name:
class Box { private: int Length; int Width; char * Name; public: Box(); ... (lots of things)... bool operator ==(const Box &) const; };
Here the space for the Name is allocated dynamically. We will assume this is a C-style string.
Now the equality operator will have more work to do. It needs to compare the Names as well.
bool Box::operator ==(const Box & Other) const { int M, N; M = strlen(Name); if (M != strlen(Other.Name)) return false; else { N = 0; while (N < M && Name[N] == Other.Name[N]) N++; if (N == M) return (Length == Other.Length && Width == Other.Width); else return false; } }
Whenever a class has data members that are pointers, we need to worry about problems of this kind. It is not enough to compare the pointers themselves; we need to check the value to which the pointers point.
What about other comparison operators?
It should be obvious how we can have an inequality operator.
For some classes, we can also have "less than" and "greater than" and so on. The same principles apply.
Other ways to code the operators
Instead of making operator == a method of the Box class, we could make it a friend of the class. In the class definition, we would have a prototype statement like this:
friend bool operator ==(const Box &, const Box &);
and in the code file we would have
bool operator ==(const Box & First, const Box & Second) { return (First.Length == Second.Length && First.Width == Second.Width); }
As a friend function, the operator will have access to Length and Width (which are private data members).
If we had access methods for Box, such as (in the class definition):
int GetLength() const; int GetWidth() const;
then we could define operator == as a stand-alone function. Its prototype would not have to be in the class definition and could be somewhere else:
bool operator ==(const Box &, const Box &);
and the code for it would use the access functions:
bool operator ==(const Box & First, const Box & Second) { return (First.GetLength() == Second.GetLength() && First.GetWidth() == Second.GetWidth()); }