2

Possible Duplicate:
Operator[][] overload

I've looked on the internet but can't find a definitive answer to this question so here I am.

I know that: operator[][]() is not a valid operator overloading, but is their a way to overload the [][] operator (used on vector<vector<typename>> for example).

I need this because I'm doing a sort of wrapper class which contains a vector of vector, and I would have liked

Community
  • 1
  • 1
TurnsCoffeeIntoScripts
  • 3,868
  • 8
  • 41
  • 46

1 Answers1

8

Yes, but it depends on the return type of operator []. You can return a type that itself supports operator [].

Let's say:

struct Matrix
{
   vector<vector<int> > x;
   vector<int>& operator[] (int i)
   {
      return x[i];
   }
};

Because x[i] return a vector, you can use [] again because vector has an operator[].

Luchian Grigore
  • 253,575
  • 64
  • 457
  • 625