c++: Can I overload operator[][]?

Viewed 55

This is a general question about operator overloading in cpp.
Is there a way to overload operator[][] ?

I want to do this because I want to implement a Matrix class, where the user can do matrix[a][b] and access the element in that position.

My friend told me to represent the matrix like this:

vector<vector<T>>

And in the constructor I would initialize it like this:

Matrix(int row, int col): vector<vector<T>>(row, vector<T>(col)) {}

Can someone please explain what the constructor is doing?
And how would I implement accessing a specific element in my Matrix, so: matrix[a][b] = c

Edit: Seems like the operator[][] works fine, so i can do matrix[a][b] and access the element there.
Why is this?

Edit 2: Thanks for the suggestions to use () but according to the instructions, it needs to be able to work on this code: enter image description here

0 Answers
Related