本文介绍了如何在没有“new”的情况下声明一个对象数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 29岁程序员,3月因学历无情被辞! class MyClass { int array_size; HisClass ** hisObject; }; 我希望hisObject指向一个HisClass对象数组。 数组的大小由array_size给出。 我的问题是,如果类型为HisClass的对象必须由 库提供函数CreateObject()声明,并且不能通过声明使用new,我应该如何声明他的对象? 如果我在构造函数中执行此操作,编译器不会抱怨,但是我 得到分段错误: array_size = 5; for(int i = 0; i< array_size; i ++){ hisObject [i] = CreateObject(...); } 提前致谢!Hi,class MyClass{int array_size;HisClass **hisObject;};I want hisObject to point to an array of HisClass objects.The size of the array is given by array_size.My question is, if objects of type HisClass must be declared by thelibrary provided function CreateObject(), and cannot be declared byusing "new", how should I declare hisObject?If I do this in the constructor, the compiler does not complain, but Iget Segmentation fault:array_size = 5;for (int i = 0; i < array_size; i++){hisObject[i] = CreateObject(...);}Thanks in advance!推荐答案 编译器不会抱怨,因为它假设你做了正确的事情并分配了内存。但是,在运行时,您的应用程序会尝试访问不允许使用的内存或不是有效地址,因此 您将获得seg错误。 /> Lionel。The compiler won''t complain because it assumes you have done the rightthing and allocated the memory. However, at run time your app tries toaccess memory that it is not allowed to or isn''t a valid address and soyou get the seg fault.Lionel. 您是否考虑过使用std :: vector< HisClass *>相反? DWHave you considered using a std::vector<HisClass *> instead?DW 从OP的帖子看,array_size不是编译器 - 时间常数。 DWIt looks from the OP''s post that array_size is not a compile-time constant.DW 这篇关于如何在没有“new”的情况下声明一个对象数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 上岸,阿里云! 08-04 11:43