在我的主要功能之前,我声明了一个静态指针数组,其中包含指向实现基本数组类的对象的新指针,并且我想知道是否应该使用“删除”自己删除它(或仅删除类)。
static AStudentList* a = new AStudentList();
static BStudentList* b = new BStudentList();
static CStudentList* c = new CStudentList();
static DStudentList* d = new DStudentList();
static InstitutionStudentList* instArr[4] = {a, b, c, d};
int main(int argc, char *argv[])
{
}
您需要delete
在程序结束之前对四个对象分别进行操作,如下所示:
delete instArr[0];
delete instArr[1];
delete instArr[2];
delete instArr[3];
您不应该delete
使用数组,因为它不是动态分配的。
但是,我看不出在这里使用动态分配对象的理由。为什么不做:
static AStudentList a;
static BStudentList b;
static CStudentList c;
static DStudentList d;
static InstitutionStudentList* instArr[4] = {&a, &b, &c, &d};
那你就没事了delete
。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句