Example: suppose we have a TMatrix class, in which an instantiated object is encapsulated a two-dimensional array of int elements. A possible solution for obtaining its specific element is the getEntrie(int row, int col) function getEntrie(int row, int col) returns an element by row and column number.

Another solution is to create a nested class, for example TPtrRow , which stores a pointer to an int . Then, we overload the [] operator for the TMatrix class so that it returns the TPtrRow object that holds the string, the number of which we passed when calling []. Similarly, we overload the operator [] for the class TPtrRow - then using two operators in a row obj[row][col] we get the element we need.

Actually, the question is: how can such a construction be implemented to those cases when we are dealing with N-dimensional arrays, N> 2?

  • Summarize TPtrRow so that each successive call returns a matrix class with a dimension less than the initial one. - ߊߚߤߘ
  • @Arhad, do you mean to make TPtrRow template? - isnullxbh
  • It is possible and so, and parameterize the template dimension of the returned submatrices. For a one-dimensional matrix, define template specialization, which returns the total value of the cell instead of the next matrix. - ߊߚߤߘ
  • @Arhad, judging by your "possible and so ..", did you want to offer something else?) - isnullxbh
  • Instead of a chain of operators [] use the operator () , which takes all the measurements at once. - ߊߚߤߘ

1 answer 1

Well, you can conjure a little with variadic templates, if you have C ++ 11 at your disposal:

 #include <iostream> template<class T, int size> class Row{ public: typedef T ValueType; private: ValueType _items[size]; public: ValueType& operator[](int index){ return _items[index]; } }; template<class T, int ...sizes> struct Table{ template<class TR, int sizeR, int ...sizesR> struct Rebind{ typedef Row<typename Rebind<TR, sizesR...>::Type, sizeR> Type; typedef Row<typename Rebind<TR, sizeR, sizesR...>::Type, sizeR> ValueType; }; template<class TR, int sizeR> struct Rebind<TR, sizeR>{ typedef Row<TR, sizeR> Type; typedef typename Type::ValueType ValueType; }; typedef typename Rebind<T, sizes...>::Type Items; public: typedef typename Items::ValueType ValueType; private: Items _items; public: ValueType& operator[](int index){ return _items[index]; } }; int main(){ Table<int, 5, 2, 4, 5, 9> table; table[3][1][3][3][7] = 42; std::cout << table[3][1][3][3][7]; } 

I think this code can be somehow simplified using using from C ++ 11, but I honestly never used it :)

PS: As for me, it's easier to zaipipdefit a few nested lines

 #include <iostream> template<class T, int size> class Row{ public: typedef T ValueType; private: ValueType _items[size]; public: ValueType& operator[](int index){ return _items[index]; } }; template<class T> using Table = Row<Row<Row<Row<Row<T, 9>, 5>, 4>, 2>, 5>; int main(){ Table<int> table; table[3][1][3][3][7] = 42; std::cout << table[3][1][3][3][7]; } 
  • Elite, yes ....)) - isnullxbh