小编Jan*_*net的帖子

如何初始化结构中的数组而不单独执行每个元素?(C++)

我的问题在代码中,但基本上我想知道如何/如果我可以做两个注释掉的行?我知道我可以在构造函数中完成它,但我不想!

struct foo
{
    int b[4];
} boo;

//boo.b[] = {7, 6, 5, 4}; // <- why doesn't this work? (syntax error : ']')
//boo.b = {7, 6, 5, 4}; // <- or else this? (syntax error : '{')

boo.b[0] = 7; // <- doing it this way is annoying
boo.b[1] = 6; // :
boo.b[2] = 5; // :
boo.b[3] = 4; // <- doing it this way is annoying

boo.b[4] = 3; // <- why does this work! …
Run Code Online (Sandbox Code Playgroud)

c++ arrays struct

2
推荐指数
1
解决办法
331
查看次数

标签 统计

arrays ×1

c++ ×1

struct ×1