Yeah, you need to think about it the other way.
What line 17 means in your code is:
"I, Bag, can check if I am equal to the Contact called rhs."(and it can be checked by this code:
1 2 3 4
|
Bag bag;
Contact contact;
if(bag == contact)
std::cout<<"Yay!";
|
However, two things will not work:
1)You want to be able to check if Contact1 is equal to Contact2
2)You want to be able to check if Contact2 is equal to Contact1
Both tests would fail the code above, but moreover, if I wrote it this way:
1 2
|
//code...
if(contact == bag) // Error! You don't have an bool Contact::operator==(Bag& other) const; !
|
You see? But yet you can do this:
1 2 3
|
int i, j;
i == j;
j == i;//both legit
|
I don't want to be your tutorial book; if you read deeply about pointers, references and classes, then you should know that what I did with int was available to me thanks to references.
But, back to topic: how can you check if Contact1 is the same as Contact2?(and the other way?)
Well, let's go step by step.
It would be natural to write a simple function.
For clarity, let's define Contact class, and then our function. Also, for clarity, I will omit getters and setters; I will just make data public. You shouldn't do that, but you should already know it :)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
|
class Contact
{
public:
int ID;//Contact's ID
int num;//Contact's telephone number
std::string name;//Contact's name
Contact(int id = 0, int nu = 555, std::string nam = "Tom") : ID(id), num(nu), name(nam)
{}//Default and not default constructor
};
//Okay, so how do we check if both Contacts are the same? Simply check if their fields(ID, num, name) are the same:
bool AreContactsTheSame(const Contact& first, const Contact& second)
{
bool result = true;
if((first.num != second.num) || (first.name != second.name))//I skip ID, because the same person may occupy two fields in our database
result = false;//If any of the fields isn't the same in both contacts, it means that contacts aren't the same
return result;
}
|
Okay, that was pretty straightforward. But it would also be annoying as hell if you had to write AreContactsTheSame every time you want to compare 2 contacts, right? That's when we go to definition of Contact, and add an == operator.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
|
//I just copied class definition from above and added operator
class Contact
{
public:
int ID;//Contact's ID
int num;//Contact's telephone number
std::string name;//Contact's name
Contact(int id = 0, int nu = 555, std::string nam = "Tom") : ID(id), num(nu), name(nam)
{}//Default and not default constructor
bool operator== (Contact& other) const
{
return AreContactsTheSame(*this, other);
}
};
|
That's it. Notice that we don't need 2 arguments, since you call it like this:
So actually you can interpret it like that:
|
contact1.operator==(contact2);
|
Simply you have to understand, that Contact has to know how to compare itself to other contact. Bag shouldn't care about how Contact looks inside. It should have right tools to do the job, the same way as you don't define any operator== for int or double - you already have it included. And it's the int's job to know how to check if it's equal to other int.
Also, notice that previously I said that you compare "all member data", but now I didn't do it. I did not lie to you previously(well, not too much); simply when you start, and overally quite often when you program, one class is the same as other if it has all fields the same. However, sometimes it doesn't. You had an example right here - if there was a field 0 with John Smith with phone 5234, and field 538 with same John Smith with same number(because he probably had two companies, or whatever). But it's up to you to define rules to check if 2 inputs are the same.
Cheers!
PS. And sorry for it, but I did forgot one thing(and it's wiser to mention it as PS then edit code): every time you're defining an operator==, you may want to add this in the beginning:
1 2 3 4 5
|
bool operator==(someclass& other) const
{
if(this == &other)
return true;
}
|
It does not mean anything more than "If I am comparing myself to myself, then I'm the same as me". So, comparing a to a should always yield true. And if you had a large class with thousands of member data, comparing one to itself could be pretty much expensive, not mentioning pointless.
Cheers!