[英]Creating an array of a class with arguments in constructor
I'm trying to instantiate an array of a class where the constructor takes two arguments, and initialize it in the same line. 我正在尝试实例化一个类的数组,其中构造函数接受两个参数,并在同一行初始化它。
Conceptually, I want to do something like this: 从概念上讲,我想做这样的事情:
foo::foo (int A, int B = 10)
{
V1 = A;
V2 = B;
}
foo Myfoo[3] = { (1, 100), (2, 300), (5, 100) };
// what I _don't_ want to do is create individual objects like this:
// foo Myfoo1(1, 100);
// foo Myfoo2(2, 300);
// foo Myfoo3(5, 100);
What I found is that when the constructor is called the arguments are not as expected. 我发现,当调用构造函数时,参数不符合预期。 The B argument always shows up as the default value of 10. Just in tinkering I threw in additional arguments in the array initialization.
B参数总是显示为默认值10.就在修补时,我在数组初始化中添加了其他参数。
foo Myfoo[3] = { (0, 1, 100), (2, 300, 0), (0, 5, 100, 0) };
To my surprise it compiled without error, but I didn't pursue this too far, because it didn't make sense to me - but I was able to affect the problem 令我惊讶的是它编译没有错误,但我没有追求太远,因为它对我没有意义 - 但我能够影响问题
Does anyone have an idea on how I should code this? 有没有人知道我应该如何编码? I've already worked around the problem but I'm curious how it should be done properly.
我已经解决了这个问题,但我很好奇它应该如何正确完成。
With the usage of (1, 100)
, you're just passing one int
with value 100
to the constructor of foo
. 使用
(1, 100)
,你只需要将一个值为100
int
传递给foo
的构造函数。 The comma-operator just discards the 1st operand, and returns the 2nd operand here. 逗号运算符只丢弃第一个操作数,并在此处返回第二个操作数。 (It doesn't work in the way as you expected like
foo Myfoo1(1, 100);
or foo(1, 100);
.) (它不像你
foo Myfoo1(1, 100);
那样工作,如foo Myfoo1(1, 100);
或foo(1, 100);
foo Myfoo1(1, 100);
)
You should use {}
( list initialization (since C++11) ) instead of ()
, ie 您应该使用
{}
( 列表初始化(自C ++ 11) )而不是()
,即
foo Myfoo[3] = { {1, 100}, {2, 300}, {5, 100} };
The cause of the problem has already been explaind by @songhuanyao . @songhuanyao已经解释了问题的原因。
I can think of the following ways to resolve the problem. 我可以想到以下解决问题的方法。
Use {}
instead of ()
to construct objects. 使用
{}
而不是()
来构造对象。
foo Myfoo[3] = { {1, 100}, {2, 300}, {5, 100}};
Use foo
explitly with ()
to construct objects. 使用
foo
explitly with ()
来构造对象。
foo Myfoo[3] = { foo(1, 100), foo(2, 300), foo(5, 100) };
Use foo
explitly with {}
to construct objects. 使用
foo
explitly with {}
来构造对象。
foo Myfoo[3] = { foo{1, 100}, foo{2, 300}, foo{5, 100}};
We need to use {}
instead of ()
and code is working as expected 我们需要使用
{}
而不是()
,代码按预期工作
#include<iostream>
using namespace std;
class Test
{
public:
Test(int a, int b = 10)
{
x = a;
y = b;
cout << "C-tor" << endl;
}
void show()
{
cout << " x = " << x << " y = " << y << endl;
}
private:
int x;
int y;
};
int main()
{
Test obj[3] = {{10,20},{30,40}, {50,60}};
obj[0].show();
obj[1].show();
obj[2].show();
return 0;
}
Above code produces expected output: 以上代码产生预期输出:
C-tor
C-TOR
C-tor
C-TOR
C-tor
C-TOR
x = 10 y = 20
x = 10 y = 20
x = 30 y = 40
x = 30 y = 40
x = 50 y = 60
x = 50 y = 60
I hope it helps! 我希望它有所帮助!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.