您是否能够将指针数组的值设置为等于指向类的指针的类

编程入门 行业动态 更新时间:2024-10-28 02:26:17
本文介绍了您是否能够将指针数组的值设置为等于指向类的指针的类的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

假设有一个功能程序围绕此工作将进行以下工作:

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; }

更多推荐

您是否能够将指针数组的值设置为等于指向类的指针的类

本文发布于:2023-07-29 08:25:26,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1239054.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:指针   数组   设置为   您是否

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!