<tfoot id='VTeyp'></tfoot>

  • <i id='VTeyp'><tr id='VTeyp'><dt id='VTeyp'><q id='VTeyp'><span id='VTeyp'><b id='VTeyp'><form id='VTeyp'><ins id='VTeyp'></ins><ul id='VTeyp'></ul><sub id='VTeyp'></sub></form><legend id='VTeyp'></legend><bdo id='VTeyp'><pre id='VTeyp'><center id='VTeyp'></center></pre></bdo></b><th id='VTeyp'></th></span></q></dt></tr></i><div id='VTeyp'><tfoot id='VTeyp'></tfoot><dl id='VTeyp'><fieldset id='VTeyp'></fieldset></dl></div>
    <legend id='VTeyp'><style id='VTeyp'><dir id='VTeyp'><q id='VTeyp'></q></dir></style></legend>
      <bdo id='VTeyp'></bdo><ul id='VTeyp'></ul>

      <small id='VTeyp'></small><noframes id='VTeyp'>

        如何在 C++ 中初始化嵌套结构?

        时间:2024-08-14
          <tbody id='q4yQf'></tbody>

            <i id='q4yQf'><tr id='q4yQf'><dt id='q4yQf'><q id='q4yQf'><span id='q4yQf'><b id='q4yQf'><form id='q4yQf'><ins id='q4yQf'></ins><ul id='q4yQf'></ul><sub id='q4yQf'></sub></form><legend id='q4yQf'></legend><bdo id='q4yQf'><pre id='q4yQf'><center id='q4yQf'></center></pre></bdo></b><th id='q4yQf'></th></span></q></dt></tr></i><div id='q4yQf'><tfoot id='q4yQf'></tfoot><dl id='q4yQf'><fieldset id='q4yQf'></fieldset></dl></div>

            1. <tfoot id='q4yQf'></tfoot>
                  <bdo id='q4yQf'></bdo><ul id='q4yQf'></ul>
                • <legend id='q4yQf'><style id='q4yQf'><dir id='q4yQf'><q id='q4yQf'></q></dir></style></legend>

                • <small id='q4yQf'></small><noframes id='q4yQf'>

                • 本文介绍了如何在 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++ 中初始化嵌套结构?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  上一篇:C++:嵌套映射 下一篇:为什么不是嵌套函数的想法,在旧的 c++ 标准中实现?

                  相关文章

                  <small id='qeuNX'></small><noframes id='qeuNX'>

                • <tfoot id='qeuNX'></tfoot>
                    • <bdo id='qeuNX'></bdo><ul id='qeuNX'></ul>
                    <i id='qeuNX'><tr id='qeuNX'><dt id='qeuNX'><q id='qeuNX'><span id='qeuNX'><b id='qeuNX'><form id='qeuNX'><ins id='qeuNX'></ins><ul id='qeuNX'></ul><sub id='qeuNX'></sub></form><legend id='qeuNX'></legend><bdo id='qeuNX'><pre id='qeuNX'><center id='qeuNX'></center></pre></bdo></b><th id='qeuNX'></th></span></q></dt></tr></i><div id='qeuNX'><tfoot id='qeuNX'></tfoot><dl id='qeuNX'><fieldset id='qeuNX'></fieldset></dl></div>
                    <legend id='qeuNX'><style id='qeuNX'><dir id='qeuNX'><q id='qeuNX'></q></dir></style></legend>