booloperator==(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.
// 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){}
booloperator ==(const Foo &other) const
{
if (this->m_int == other.m_int &&
this->m_char == other.m_char)
{
returntrue;
}
returnfalse;
}
};
// 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. ¬_¬
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.