如何在没有“new”的情况下声明一个对象数组 [英] how to declare an array of objects without "new"

查看:65
本文介绍了如何在没有“new”的情况下声明一个对象数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述




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 the
library provided function CreateObject(), and cannot be declared by
using "new", how should I declare hisObject?

If I do this in the constructor, the compiler does not complain, but I
get Segmentation fault:

array_size = 5;
for (int i = 0; i < array_size; i++){
hisObject[i] = CreateObject(...);
}

Thanks in advance!

推荐答案

b8 ******* @ yahoo.com 写道:


类MyClass {
int arra y_size;
HisClass ** hisObject;
};


你所做的一切都是指向一无所有的指针,它没有分配任何

的空间。


我希望hisObject指向一个HisClass对象数组。


你想要的是一系列指向HisClass对象的指针:

HisClass * hisObject [array_size];


这将创建一个长度为array_size的数组,每个元素

允许足够的空间用于指向HisClass对象的指针大小。

数组的大小由array_size给出。

我的问题是,如果类型为HisClass的对象必须由提供的函数CreateObject()声明,并且不能由<使用new,我应该如何声明他的对象?

如果我在构造函数中执行此操作,编译器不会抱怨,但是我得到了Segmentation fault:

array_size = 5;
for(int i = 0; i< array_size; i ++){
hisObject [i] = CreateObject(...);
}
Hi,

class MyClass{
int array_size;
HisClass **hisObject;
};
All you''ve done is created a pointer to nothing, it''s not allocated any
space.

I want hisObject to point to an array of HisClass objects.
What you want is an array of pointers to HisClass objects:

HisClass *hisObject[array_size];

This will create an array of length array_size, with each element
allowing enough space for the size of a pointer to a HisClass object.


The size of the array is given by array_size.

My question is, if objects of type HisClass must be declared by the
library provided function CreateObject(), and cannot be declared by
using "new", how should I declare hisObject?

If I do this in the constructor, the compiler does not complain, but I
get Segmentation fault:

array_size = 5;
for (int i = 0; i < array_size; i++){
hisObject[i] = CreateObject(...);
}




编译器不会抱怨,因为它假设你做了正确的
事情并分配了内存。但是,在运行时,您的应用程序会尝试访问不允许使用的内存或不是有效地址,因此

您将获得seg错误。 />
Lionel。



The compiler won''t complain because it assumes you have done the right
thing and allocated the memory. However, at run time your app tries to
access memory that it is not allowed to or isn''t a valid address and so
you get the seg fault.
Lionel.


b8 ***** **@yahoo.com 写道:


类MyClass {
int array_size;
HisClass ** hisObject;
};

我希望hisObject指向一个HisClass对象数组。

数组的大小由array_size给出。
我的问题是,如果类型HisClass的对象必须由
库提供的函数CreateObject()声明,并且不能使用new声明
,我该如何声明他的对象?

如果我在构造函数中执行此操作,编译器不会抱怨,但我得到Segmentation错误:


因为他的对象可能指向任何地方。 />
array_size = 5;


hisObject = new HisClass * [array_size];

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 the
library provided function CreateObject(), and cannot be declared by
using "new", how should I declare hisObject?

If I do this in the constructor, the compiler does not complain, but I
get Segmentation fault:
Because hisObject could be pointing anywhere.
array_size = 5;
hisObject = new HisClass *[array_size];
for (int i = 0; i < array_size; i++){
hisObject[i] = CreateObject(...);
}




您是否考虑过使用std :: vector< HisClass *>相反?


DW



Have you considered using a std::vector<HisClass *> instead?

DW


Lionel写道:
你想要的是一个指针数组HisClass对象:

HisClass * hisObject [array_size];
What you want is an array of pointers to HisClass objects:

HisClass *hisObject[array_size];




从OP的帖子看,array_size不是编译器 - 时间常数。


DW



It looks from the OP''s post that array_size is not a compile-time constant.

DW


这篇关于如何在没有“new”的情况下声明一个对象数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆