'can't figure out this kind of type in C++ (array of struct?) [closed]
If I have a struct like this
struct my_struct
{
float a, b;
};
Is there a way to declare an object by doing so?
my_struct[10][10] my_struct_inst;
If this is allowed, could anyone teach me how to understand this? Or what's the concept of this code?
So many thanks in advance.
Solution 1:[1]
Your syntax is simply wrong. Array size goes after the variable name, not the type:
my_struct my_struct_inst[10][10];
Note that the type of my_struct_inst is still my_struct[10][10], that's just the syntax for variable definitions.
With modern C++, I would consider using std::array instead, though that's not pretty either:
std::array< std:array<my_struct, 10>, 10> my_struct_inst;
This is basically same thing and equally efficient, but it also offers a bunch of nice methods, which may be useful.
To make that easier, you can define it as a type:
using my_struct_10x10 = std::array< std:array<my_struct, 10>, 10>;
my_struct_10x10 my_struct_inst;
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | hyde |
