带参数和数组的对象 [英] Objects with arguments and array

查看:27
本文介绍了带参数和数组的对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在 C++ 中有没有一种方法可以在对象上添加参数,例如:

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

我希望我可以将参数放入对象中,同时创建一个包含 5 个这些对象的数组,有谁知道怎么做?有没有更好的方法?

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?

推荐答案

在 C++ 中构造对象数组时,除非使用显式数组初始化语法,否则只能使用默认构造函数:

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

以下是 C++ 常见问题解答中关于此问题的一些很好的信息:

Here's some good information from the C++ FAQ about this:

http://www.parashift.com/c++-faq-lite/ctors.html#faq-10.5

这篇关于带参数和数组的对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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