it is possible to overload somehow operator for multidimensional array?
Something like:
class A {
...
int& operator[][] (const int x, const int y);
...
}
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
Nope, that is not possible. There are two alternatives, though:
You can have
operator[]return an array of a smaller dimension (For a 3D array, it will return a 2D array, for a 2D array it will return a 1D array, and for a 1D array, it will return a single element). Then you can “string them together” with the syntax you want. (arr[x][y][z])Alternatively, you can overload
operator(), because that can take multiple arguments.Then you can use it like this, to index into a 3D array for example:
arr(x,y,z)But you can’t overload
[][]or[][][]as a single operator.