My question is how i can to overloading operator [][] ? In addition what happening Behind the Scenes when i call to the extension of the operator?
-
1Neither of those are single operators. – Joey Oct 10 '17 at 12:00
-
Related: https://stackoverflow.com/questions/1642028/what-is-the-operator-in-c?rq=1 – Murphy Oct 10 '17 at 12:09
-
BTW, this question smells like a [XY problem](http://xyproblem.info). You may get more useful answers when you tell what you're trying to achieve. – Murphy Oct 10 '17 at 12:12
-
1Please don't "overload" `--->`. You could maybe do it with an `operator--(int)` that returns a proxy that has `operator->`, but such unusual syntax is highly confusing. – aschepler Oct 10 '17 at 12:13
-
@Murphy i try to find a general solution for that problem. i work on project which uses in matrixes with diffrent dimensions, i found solution for the 2-demnsions problem but for the n'th demension problem i dont find and my solution dont help me to understand how to extend it for n'th dimension – Bizzu Oct 10 '17 at 12:19
2 Answers
The correct term is operator overloading.
To implement some sort of [][]
requires your returning a proxy object for the first []
, on which the operator []
is again overloaded.
Chains of ()()
can be done in a similar way.
For the slide operator genre, see What is the "-->" operator in C++?

- 231,907
- 34
- 361
- 483
-
thanks, do you know what happening behind the scene? for the ability to extend it for **n**'th [] – Bizzu Oct 10 '17 at 12:08
-
1Well, the proxy object could be a template templatised on the *degree". This could get quite messy though. Why not use `()` with a variable argument list? – Bathsheba Oct 10 '17 at 12:09
-
1If they ever expand `operator[]` to accept multiple arguments for user defined types, I'll be a happy camper – StoryTeller - Unslander Monica Oct 10 '17 at 12:15
-
The `[]` solution is better if the intermediate objects make sense. Eg if a 2D array's `operator[]` returns a reference to a 1D array that is fully functional on its own. If you would have to create proxy objects, I prefer the use of `operator()`. – patatahooligan Oct 10 '17 at 12:17
-
1@patatahooligan - I don't like it. It feels weird to overload the function call operator on something that isn't a functor... Though I suppose one could argue that a matrix is a function from ordered pairs of indices to the underlying field... so IDK. – StoryTeller - Unslander Monica Oct 10 '17 at 12:19
-
@Bathsheba A view object is what I was thinking of too. What I mean is that it should offer full functionality, such as being copied from or assigned to/from a 1D array. I don't like solutions where the proxy objects only exists to provide `operator[]` so that you can write `array2D[x][y]`, but `array2D[x]` doesn't do what you would expect it to. – patatahooligan Oct 10 '17 at 12:27
-
@StoryTeller Fair enough, some people like `operator()` to only mean function. You can generalize my stance to: I would prefer a non-`operator[]` solution to an `operator[]` that returns a proxy object whose only functionality is to offer an `operator[]` of its own. – patatahooligan Oct 10 '17 at 12:31
The operators [][]...
, ()()...
, and -->
(as for the last, I am assuming you did not mean --->
) are not actually c++ operators, but a combination of c++ operators.
For example [][][]
, applies []
to the lvalue, and returns a certain lvalue, to which []
is applied again, and then the same for the third time. So there is no actual [][][]
, but it is simply []
applied 3 times.
int a[2] = {0,1};
int b[3] = {2,3,4};
int *c[2] = {a,b};
c[0][1]; // [0] first extracts 'a' from 'c', then [1] extracts 1 from a (the value in the second index).
The same goes for ()()()
.
-->
Also follows the same idea, except that here it is not a repeated combination of the same operator, but a combination of --
, which will return the lvalue and then decrease it by 1, and >
, which will compare the left hand side with the right hand side.

- 493
- 5
- 16