从指针数组中删除一个对象

Remove an object from an array of pointers

我有一个指向 Building 对象的指针数组,如下所示:

class Building {
    int id ;
    int year;
    double price;
    char* adress;
};

我想做的是使用 operator- 通过 id 从数组中删除对象,但我真的不知道该怎么做。我的列表 class 如下所示:

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;

我知道有更简单的方法可以做到这一点,比如使用 std::vector,但这是一个赋值,我必须使用重载的 operator- 和一个指针数组来完成它, Building class 必须看起来像那样。

我还有 operator+ 可以向数组添加一个元素,效果很好。

您的 operator- 未正确实施。尽管查找对象的循环没有问题,但删除该对象会被破坏。您没有正确更新数组,因为您需要在找到的索引之后向下移动 ALL 数组元素,而您没有这样做。

此外,请确保您的 List class 实现了 Rule of 3/5/0 correctly. The return value of operator- is supposed to return a new List object, so a copy has to be made. You should not be modifying the this object at all in operator- (that is the job of operator-= to do; see What are the basic rules and idioms for operator overloading?).

此外,buildingNr 成员不应该是 static。这会阻止您一次使用多个 List 对象。

尝试更像这样的东西:

#include <algorithm>

class List {
    Building* buildingList[10];
    int buildingNr;
    
public:
    
    List() : buildingNr(0) {}

    List(const List &src) : buildingNr(src.buildingNr) {
        for(int i = 0; i < buildingNr; ++i) {
            buildingList[i] = new Building;
            buildingList[i]->id = src.buildingList[i]->id;
            buildingList[i]->year = src.buildingList[i]->year;
            buildingList[i]->price = src.buildingList[i]->price;
            buildingList[i]->adress = new char[strlen(src.buildingList[i]->adress)+1);
            strcpy(buildingList[i]->adress, src.buildingList[i]->adress);
        }
    }

    ~List() {
        for(int i = 0; i < buildingNr; ++i) {
            delete[] buildingList[i]->adress;
            delete buildingList[i];
        }
    }

    List& operator=(const List &rhs) {
        if (this != &rhs) {
            List tmp(rhs);
            std::swap_ranges(buildingList, buildingList+10, tmp.buildingList);
            std::swap(buildingNr, tmp.buildingNr);
        }
        return *this;
    }

    List& operator-=(int id) {    
        for(int i = 0; i < buildingNr; ++i) {
            if (buildingList[i]->getId() == id) {
                delete buildingList[i];
                for(int j = i + 1; j < buildingNr; ++j) {
                    buildingList[j - 1] = buildingList[j];
                }
                buildingList[buildingNr] = NULL;
                --buildingNr;
                cout << "Building " << i << " was deleted." << endl;
                return *this;
            }
        }
        throw -1;
    } 

    ...
};

friend List operator-(List lhs, int id) {    
    lhs -= id;
    return lhs;
}