[英]How to make a struct of structs in C++
Can a struct contain other structs? 结构可以包含其他结构吗?
I would like to make a struct that holds an array of four other structs. 我想创建一个包含四个其他结构的数组的结构。 Is this possible?
这可能吗? What would the code look like?
代码会是什么样的?
Yes, you can. 是的你可以。 For example, this struct
S2
contains an array of four S1
objects: 例如,此struct
S2
包含四个S1
对象的数组:
struct S1 { int a; };
struct S2
{
S1 the_array[4];
};
Sure, why not. 当然,为什么不呢。
struct foo {
struct {
int a;
char *b;
} bar[4];
} baz;
baz.bar[1].a = 5;
Yes, structs can contain other structs. 是的,结构可以包含其他结构。 For example:
例如:
struct sample {
int i;
char c;
};
struct b {
struct sample first;
struct sample second;
};
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.