I have an array of pointers that point to Building
objects that look like this:
class Building {
int id ;
int year;
double price;
char* adress;
};
What I'm trying to do is to remove objects from the array via the id
using operator-
, but I don't really know how to do so. My list class looks like this:
class List {
Building* buildingList[10];
static int buildingNr;
public:
List operator-(int id) {
bool exists = false;
int index;
for(int i = 0; i < buildingNr; i++)
if (buildingList[i]->getId() == id) {
exists = true;
index = i;
cout << "Building " << index << " was deleted." << endl;
}
if (exists) {
delete buildingList[index];
buildingList[index] = buildingList[buildingNr];
buildingList[buildingNr] = NULL;
buildingNr--;
}
else throw - 1;
}
};
int List::buildingNr = 0;
I know that there are far easier ways of doing this, like using std::vector
, but this is an assignment and I have to do it using the overloaded operator-
and an array of pointers, and the Building
class has to look like that.
I also have the operator+
which adds an element to the array, and that works fine.