C++ 如何在另一个结构中使用结构?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6287479/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to use a struct inside another struct?
提问by Shadi
I want to use a nested structure but i dont know how to enter data in it, for example:
我想使用嵌套结构,但我不知道如何在其中输入数据,例如:
struct A {
int data;
struct B;
};
struct B {
int number;
};
So in the main when I come to use it :
所以主要是当我开始使用它时:
int main() {
A stage;
stage.B.number;
}
Is that right if not how do i Use it??
如果不是,那是对的,我该如何使用它??
回答by Rob Kennedy
Each member variable of a struct generally has a nameand a type. In your code, the first member of A
has type int
and name data
. The second member only has a type. You need to give it a name. Let's say b
:
结构体的每个成员变量通常都有一个名称和一个类型。在您的代码中,第一个成员A
具有 typeint
和 name data
。第二个成员只有一个类型。你需要给它一个名字。让我们说b
:
struct A {
int data;
B b;
};
To do that, the compiler needs to already know what B
is, so declare that struct beforeyou declare A
.
要做到这一点,编译器的需求已经知道什么B
是,这样声明结构之前声明A
。
To access a nested member, refer to each member along the path by name, separated by .
:
要访问嵌套成员,请按名称引用路径中的每个成员,并用 分隔.
:
A stage;
stage.b.number = 5;
回答by Puppy
struct A {
struct B {
int number;
};
B b;
int data;
};
int main() {
A a;
a.b.number;
a.data;
}
回答by iammilind
struct B { // <-- declare before
int number;
};
struct A {
int data;
B b; // <--- declare data member of `B`
};
Now you can use it as,
现在你可以将它用作,
stage.b.number;
回答by paxdiablo
The struct B
within A
must have a name of some sort so you can reference it:
该struct B
范围内A
必须有某种形式的名称,以便您可以参考它:
struct B {
int number;
};
struct A {
int data;
struct B myB;
};
:
struct A myA;
myA.myB.number = 42;
回答by greatwolf
struct A
{
int data;
struct B
{
int number;
}b;
};
int main()
{
A stage = { 42, {100} };
assert(stage.data == 42);
assert(stage.b.number == 100);
}
回答by Erkki Pilving
struct TestStruct {
short Var1;
float Var2;
char Var3;
struct TestStruct2 {
char myType;
CString myTitle;
TestStruct2(char b1,CString b2):myType(b1), myTitle(b2){}
};
std::vector<TestStruct2> testStruct2;
TestStruct(short a1,float a2,char a3): Var1(a1), Var2(a2), Var3(a3) {
testStruct2.push_back(TestStruct2(0,"Test Title"));
testStruct2.push_back(TestStruct2(4,"Test2 Title"));
}
};
std::vector<TestStruct> testStruct;
//push smthng to vec later and call
testStruct.push_back(TestStruct(10,55.5,100));
TRACE("myTest:%s\n",testStruct[0].testStruct2[1].myTitle);
回答by cellie
I have the somewhat like the following code running for a while live now and it works.
我现在有点像下面的代码运行了一段时间,它可以工作。
//define a timer
struct lightTimer {
unsigned long time; //time in seconds since midnight so range is 0-86400
byte percentage; // in percentage so range is 0-100
};
//define a list of timers
struct lightTable {
lightTimer timer[50];
int otherVar;
};
//and make 5 instances
struct lightTable channel[5]; //all channels are now memory allocated
@zx485: EDIT: Edited/cleaned the code. Excuse for the raw dump.
@zx485:编辑:编辑/清理代码。原谅原始转储。
Explanation:
解释:
Define a lightTimer. Basically a struct that contains 2 vars.
定义一个 lightTimer。基本上是一个包含 2 个变量的结构。
struct lightTimer {
Define a lightTable. First element is a lightTimer.
定义一个 lightTable。第一个元素是 lightTimer。
struct lightTable {
Make an actual (named) instance:
制作一个实际的(命名的)实例:
struct lightTable channel[5];
We now have 5 channels with 50 timers.
我们现在有 5 个通道和 50 个定时器。
Access like:
访问如:
channel[5].timer[10].time = 86400;
channel[5].timer[10].percentage = 50;
channel[2].otherVar = 50000;