如何初始化一个对象的数组,其构造函数需要两个或多个参数? [英] How can I initialize an array of objects whose constructor require two or more arguments?
本文介绍了如何初始化一个对象的数组,其构造函数需要两个或多个参数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
建议我们有一个A类对象的数组,A类的构造函数需要两个参数,如下所示:
suggest we have an array of class A's objects, and class A's constructor require two arguments, like this:
class A
{
public:
A( int i, int j ) {}
};
int main()
{
const A a[3] = { /*How to initialize*/ };
return 0;
}
如何初始化数组?
推荐答案
说明:
const A a[3] = { {0,0}, {1,1}, {2,2} };
在旧版本的编译器上,假设 A
一个可访问的复制构造函数,你必须说:
On older compilers, and assuming A
has an accessible copy constructor, you have to say:
const A a[3] = { A(0,0), A(1,1), A(2,2) };
C ++对于数组非常不足(某些初始化根本不可能)这在C ++ 11中有一点好处。
C++ used to be pretty deficient with respect to arrays (certain initializations just were not possible at all), and this got a little better in C++11.
这篇关于如何初始化一个对象的数组,其构造函数需要两个或多个参数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文