Array of structs and new / delete

后端 未结 8 903
没有蜡笔的小新
没有蜡笔的小新 2021-01-02 18:47

I have a struct like this:

class Items 
{
private:
    struct item
    {
        unsigned int a, b, c;
    };
    item* items[MAX_ITEMS];
}

8条回答
  •  春和景丽
    2021-01-02 19:21

    Just to be clear: you refer to calling "delete[]". I think you mean delete.

    I mention this because C++ has two separate operators, operator delete and operator delete[]. The latter is used for deleting arrays of objects allocated with operator new[], and does not apply in this case. You have an array of pointers to objects, which you must have initialised with repeated calls to operator new rather than a single call to operator new[].

    All I'm really trying to say is: your use of delete[] is confusing and ambiguous; change it to delete.

提交回复
热议问题