如何在 C++ 中初始化嵌套结构? [英] How to initialize nested structures in C++?

查看:49
本文介绍了如何在 C++ 中初始化嵌套结构?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在一个程序中创建了几个不同的结构.我现在有一个带有嵌套结构的结构,但是我无法弄清楚如何正确初始化它们.下面列出了这些结构.

I have created a couple of different structures in a program. I now have a structure with nested structures however I cannot work out how to initialize them correctly. The structures are listed below.

/***POINT STRUCTURE***/
struct Point{
    float x;                    //x coord of point
    float y;                    //y coord of point
};

/***Bounding Box STRUCTURE***/
struct BoundingBox{
    Point ymax, ymin, xmax, xmin;
};

/***PLAYER STRUCTURE***/
struct Player{
    vector<float> x;            //players xcoords
    vector<float> y;            //players ycoords
    BoundingBox box;
    float red,green,blue;       //red, green, blue colour values
    float r_leg, l_leg;         //velocity of players right and left legs
    int poly[3];                //number of points per polygon (3 polygons)
    bool up,down;               
};

然后我尝试初始化一个新创建的名为 player 的 Player 结构.

I then attempt to intialse a newly created Player struct called player.

//Creates player, usings vectors copy and iterator constructors
Player player = { 
vector<float>(xcords,xcords + (sizeof(xcords) / sizeof(float)) ), //xcords of player
vector<float>(ycords,ycords + (sizeof(ycords) / sizeof(float)) ), //ycoords of playe
box.ymax = 5;               //create bounding box
box.ymin = 1;
box.xmax = 5;
box.xmin = 1;
1,1,1,                      //red, green, blue
0.0f,0.0f,                  //r_leg,l_leg
{4,4,4},                    //number points per polygon
true,false};                //up, down

这会导致几个不同的错误,与框有关.声明框没有明确的标识符,并且在."之前缺少结构或语法.

This causes several different errors, concerning box. Stating the box has no clear identifier and missing struct or syntax before '.'.

然后我尝试创建一个 Player 结构并初始化它的成员如下:

I then tried just to create a Player struct and initialise it's members as follows:

Player bob;
bob.r_leg = 1;

但这会导致更多错误,因为编译器认为 bob 没有标识符或缺少某些语法.

But this causes more errors, as the compiler thinks bob has no identifier or is missing some syntax.

我用谷歌搜索了这个问题,但没有找到任何文章向我展示如何在(父)结构中初始化嵌套结构的许多不同成员.任何有关此主题的帮助将不胜感激:-) !!!

I googled the problem but I did not find any articles showing me how to initalise many different members of nested structures within the (parent) structure. Any help on this subject would be greatly appreciated :-) !!!

推荐答案

你通常用 { ... } 初始化它:

You initialize it normally with { ... }:

Player player = { 
  vector<float>(xcords,xcords + (sizeof(xcords) / sizeof(float)) ),
  vector<float>(ycords,ycords + (sizeof(ycords) / sizeof(float)) ),
  5, 1, 5, 1, 5, 1, 5, 1, 
  1.0f,1.0f,1.0f,                         //red, green, blue
  0.0f,0.0f,                              //r_leg,l_leg
  {4,4,4},                                //number points per polygon
  true,false
};   

现在,这是使用括号省略".一些编译器对此发出警告,即使它是完全标准的,因为它可能会使读者感到困惑.最好添加大括号,这样就可以清楚地了解在何处初始化:

Now, that is using "brace elision". Some compilers warn for that, even though it is completely standard, because it could confuse readers. Better you add braces, so it becomes clear what is initialized where:

Player player = { 
  vector<float>(xcords,xcords + (sizeof(xcords) / sizeof(float)) ),
  vector<float>(ycords,ycords + (sizeof(ycords) / sizeof(float)) ),
  { { 5, 1 }, { 5, 1 }, { 5, 1 }, { 5, 1 } }, 
  1.0f, 1.0f, 1.0f,               //red, green, blue
  0.0f, 0.0f,                     //r_leg,l_leg
  { 4, 4, 4 },                    //number points per polygon
  true, false
};

如果您只想初始化点的 x 成员,您可以通过省略其他初始化程序来实现.聚合(数组、结构)中的剩余元素将被初始化为正确"的值——因此,NULL 用于指针,false 用于 bool,零用于 int 和依此类推,并大致使用用户定义类型的构造函数.初始化点的行看起来像这样

If you only want to initialize the x member of the points, you can do so by omitting the other initializer. Remaining elements in aggregates (arrays, structs) will be value initialized to the "right" values - so, a NULL for pointers, a false for bool, zero for ints and so on, and using the constructor for user defined types, roughly. The row initializing the points looks like this then

{ { 5 }, { 5 }, { 5 }, { 5 } }, 

现在您可以看到使用大括号省略的危险.如果向结构中添加某个成员,则所有初始值设定项都会移开"它们应该初始化的实际元素,并且它们可能会意外命中其他元素.所以你最好总是在适当的地方使用大括号.

Now you could see the danger of using brace elision. If you add some member to your struct, all the initializers are "shifted apart" their actual elements they should initialize, and they could hit other elements accidentally. So you better always use braces where appropriate.

尽管考虑使用构造函数.我刚刚完成了您的代码,展示了您将如何使用大括号括起来的初始值设定项来执行此操作.

Consider using constructors though. I've just completed your code showing how you would do it using brace enclosed initializers.

这篇关于如何在 C++ 中初始化嵌套结构?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆