0

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].)

user366202
  • 249
  • 2
  • 7
  • 3
    There is no `operator[][]`, only `operator[]`. (See [the rules for operator overloading](https://stackoverflow.com/questions/4421706/what-are-the-basic-rules-and-idioms-for-operator-overloading) for a reference.) The question you reference is the same as your question. (If your intermediate class is simple enough, the compiler should be able to optimize in your zero overhead goal.) – JaMiT Sep 17 '20 at 19:38
  • 1
    You'd be better off overloading `inline MyTypeName& operator()(int i, int j) { return array[i + j * i_dim]; }` and doing your own 2D i,j -> 1D mapping. But if you want to do it your way, you'll need another helper class with a proxy object that represents the *row view* of the matrix. – Eljay Sep 17 '20 at 19:40
  • the top answer applies for your case almost unchanged. If it looks a little complex then because it is. Simply don't use operator overloads when not appropriate. Use a method `get_element(size_t,size_t);` – 463035818_is_not_an_ai Sep 17 '20 at 19:41
  • @idclev463035818 my main concern is performance and I was under the impression that a `get` method would have some overhead compared to a simple, "C-like", [][]. Though I am still quite new to C++ so perhaps this is not correct. – user366202 Sep 17 '20 at 19:46
  • there is no "simple [][]". There is no `operator[][]`, you can only emulate it. – 463035818_is_not_an_ai Sep 17 '20 at 19:52
  • in any case a method will not be any worse than an operator overload concerning performance (unless it is, then you have to measure and see, but dont do premature optimization) – 463035818_is_not_an_ai Sep 17 '20 at 19:53
  • 2
    Overloaded operators are just functions, so behind the `[]` operator is going to be a function very much like `get_element`. You gain nothing other than less typing. And in this case that's probably eaten up by all the work you need to go through making the proxy object to get `[][]` working. If you're going to overload anything, take Eljay's advice and overload `operator()`. – user4581301 Sep 17 '20 at 21:20

0 Answers0