I have a struct containing a large 2D array, the dimensions of which are fixed and known at compile time, similar to this:
struct MyStruct{
MyTypeName array[1000][10];
.
.
.
[other stuff]
};
I want to be able to access elements of this struct as,
MyStruct a;
// some code that sets the array values
MyTypeName b = a[55][9];
but I am not sure how to overload the "[][]
" operator. In the 1D case, the following would work:
struct My1Dstruct{
MyTypeName array[1000];
inline MyTypeName& operator[](int index){
return array[index];
}
inline const MyTypeName& operator[](int index) const{
return array[index];
}
.
.
.
[other stuff]
}
but I cannot figure out how to get this working in a simple way for two dimensions.
I read this previous question, but it requires more flexibility than I do, and so the answers seem rather complex for my purposes. Is there any way to overload [][]
for the specific case of a 2D array with an explicit known size, without any more cost than accessing the array directly? (i.e. ideally I'm looking for a solution where b = a[55][9]
has zero overhead compared to b = a.array[55][9]
.)