As others have pointed out, there is no operator [][] , a [] operator applied to the results of the operator a [] . In the most general case, the first operator [] will return a proxy server that implements the operator [] . In the simplest case, a βproxyβ can be T* , since in C ++ pointers implement the [] operator. a more general implementation can be done as follows:
class ElementProxy { Container* myOwner; int myRowIndex; int myColumnIndex; public: ElementProxy( Container* owner, int rowIndex, int columnIndex ) : myOwner( owner ) , myRowIndex( rowIndex ) , myColumnIndex( columnIndex ) { } operator Type() const
This is not perfect; if you are dealing with class types, it is not possible to support something line container[i][j].x , for example; we cannot overload operator. . If you need to support this, the best thing you can do is to overload operator-> on ElementProxy and require client code to use -> instead . although this is not a pointer, smart or otherwise.
source share