本文介绍了您是否能够将指针数组的值设置为等于指向类的指针的类的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
假设有一个功能程序围绕此工作将进行以下工作:
Assuming that there is a functional program built around this will the following work:
Item** items; Item* item; int nItems; items[nItems] = item;或者我必须设置每个实例的每个成员变量更像是这样的项目:
Or would I have to set each member variable of each instance of Item more like this:
int newId = item->getId(); items[i]->setID(newId);推荐答案
第一个如果变量被正确初始化,选项将有一些修改。 The first option will work with some modifications, provided that the variables are initialized properly. int nItems = 5; Item** items = (Item**)calloc(nItems, sizeof(Item*)); Item* item = new Item(); // If Item is a class items[nItems] = item; // nItems will be outside the length of the array items[0] = item; // Cleanup if (items != null) { for (int i=0; i<nitems;> { if (items[i] != null) delete items[i]; } free(items); items = null; }
更多推荐
您是否能够将指针数组的值设置为等于指向类的指针的类
发布评论