是否可以在C ++中使用初始化列表设置数组的特定成员? [英] Is it possible to set a specific member of an array with initialization list in C++?
本文介绍了是否可以在C ++中使用初始化列表设置数组的特定成员?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我知道您可以使用初始化列表初始化整个数组,如下所示:
I know that you can initialize the whole array with initialization list like this :
struct s {
int a[1];
s (int b): a{b}{}
};
,但是可以设置一个特定成员的值吗?因为这个:
, but is it possible to set value of one specific member? Because this:
struct s {
int a[1];
s (int b): a[0]{}
};
无效,并引发两个错误:
doesn't work and throws two errors:
expected '(' before '[' token
和
expected '{' before '[' token.
推荐答案
您可以通过
更新(感谢@NathanOliver) UPDATE (thanks @NathanOliver) 缺少的元素是零初始化. 这篇关于是否可以在C ++中使用初始化列表设置数组的特定成员?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!struct s {
int a[2];
s (int b): a{b} {} // a[1] is already 0
};
查看全文