您是否能够将指针数组的值设置为等于指向类的指针的类 [英] Are you able to set the value of an array of pointers to classes equal to a pointer to a class
本文介绍了您是否能够将指针数组的值设置为等于指向类的指针的类的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
假设有一个功能程序围绕此工作将进行以下工作:
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;
}
这篇关于您是否能够将指针数组的值设置为等于指向类的指针的类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文