How to initialize nested structures in C++?

╄→гoц情女王★ 提交于 2019-12-01 03:39:39

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
};

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.

You can add default values to a structure like so:

struct Point{
   Point() : x(0), y(0)
     { };

   float x;
   float y;
};

or

struct Point{
   Point() 
     { 
       x = 0;
       y = 0;
     };

   float x;
   float y;
};

For adding those values during construction, add parameters to constructors like this:

struct Point{
   Point(float x, float y) 
     { 
       this->x = x;
       this->y = y;
     };

   float x;
   float y;
};

and instantiate them with these:

Point Pos(10, 10);
Point *Pos = new Point(10, 10);

This also works for your other data structures.

It looks like the bounding box should just contain floats, not Points?

That said this might work:

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

(Untested)...

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!