带有参数和数组的对象 [英] Objects with arguments and array
问题描述
在C ++中有一种方法,其中一个对象添加了参数,并有一个数组,如:
int x = 1;
int y = 2;
对象myObject(x,y)[5]; //不工作
我希望我可以将参数放入对象,同时创建一个数组的5个这些对象,有没有人知道如何?
在C ++中构造对象数组时,只有默认的构造函数可以使用,除非你'请使用显式的Array初始化语法:
Object myObject [5] = {Object(x,y),
Object(x,y),
Object(x,y),
Object(x,y),
>
以下是C ++常见问题解答中的一些很好的信息:
http://www.parashift.com/c++-faq-lite/ctors.html#faq -10.5
Is there a way in C++ where an objects has argument added upon it, with an array such as:
int x = 1;
int y = 2;
Object myObject( x, y )[5]; // does not work
I was hoping that I could put arguments into the object, while creating an array of 5 of these objects, does anyone know how? and is there a bteter way?
解决方案 When constructing an array of objects in C++ only the default constructor can be used unless you're using the explicit Array initialization syntax:
Object myObject[5] = { Object( x, y ),
Object( x, y ),
Object( x, y ),
Object( x, y ),
Object( x, y ) }
Here's some good information from the C++ FAQ about this:
http://www.parashift.com/c++-faq-lite/ctors.html#faq-10.5
这篇关于带有参数和数组的对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!