Path: utzoo!utgpu!jarvis.csri.toronto.edu!rutgers!deimos.cis.ksu.edu!unmvax!tut.cis.ohio-state.edu!ucbvax!agate!saturn!daniel From: daniel@saturn.ucsc.edu (Daniel Edelson) Newsgroups: comp.lang.c++ Subject: Re: operator[][]? Message-ID: <7512@saturn.ucsc.edu> Date: 16 May 89 20:08:03 GMT References: Reply-To: daniel@saturn.ucsc.edu (Daniel Edelson) Distribution: comp.lang.c++ Organization: University of California, Santa Cruz Lines: 44 In article neath@solar-1.stars.flab.Fujitsu.JUNET writes: >I am trying to determine what support there is in C++ for multi-dimensional >arrays within a class. For example, the following class definition attempts to >implement an array of pointers to vectors. Now, I know that I can overload >operator[], but what I would really like to be able to do is overload the >ficticious operator[][]. How can I achieve this functionality? Am I missing >something very obvious? >Martin Neath You need the type of A to be different from the type of A[i] so that the [] operator applied to expressions of each type works correctly. Example: class Element { /* stuff */ }; class Vector { private: Element * col; // an array of elements ... public: Element & operator[](int index2); ... }; class Array{ private: Vector * rows; // an array of vectors ... public: Vector & operator[](int index1); ... }; Let A be of type Array, A[i] is of type Vector and A[i][j] is of type Element. The two instances of the subscripting operator in the expression A[i][j] are not the same implementation, but are both [] operators. daniel edelson daniel@saturn.ucsc.edu