Write a function that takes an object as argument and deletes that object from an array of objects

So I have two classes, one for contactInfo and another for contactBook. I create a few contactInfo objects and add them to listofContacts[] array in contactBook using an addContact() function which takes the object sent to it and put in the next available space in listofContacts[]. now I need to create a deleteContact() function which will delete the object passed to it from the lisofContact[]. I am unable to do that since I don't know how to compare the object passed to deleteContact() with the objects in listofContacts[].

here is the code for add and delete function i got so far.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
  void addContact(ContactInfo con)
	{
		listOfContact[objcount] = con;
		listOfContact[objcount].index = objcount;
		objcount++;
	}

	void deleteContact(ContactInfo con)
	{
		int i,j;

		for (i = 0; i <= objcount-1; i++)
		{
			if (listOfContact[i] == con)
			{
				//deletes listofContact[i] if matches with con
			}
		}	
	}
To compare 2 ContactInfo objects you can either overload the == operator inside the class or create a separate function.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
inside the class
class ContactInfo
{
bool operator==(const ContactInfo& rhs)
{
  // your logic
}
};

or outside the class

bool operator==(const ContactInfo& lhs, const ContactInfo& rhs)
{
  // your logic
}
Last edited on
One minor but important change to Thomas1965's suggestion: that operator must be const:
1
2
3
4
bool operator==(const ContactInfo &rhs) const
{
    // your logic
}

Topic archived. No new replies allowed.