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

0

1 Answer 1

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

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.