Path: utzoo!attcan!uunet!lll-winken!lll-tis!ames!mailrus!tut.cis.ohio-state.edu!bloom-beacon!gatech!ulysses!hector!jss From: jss@hector.UUCP (Jerry Schwarz) Newsgroups: comp.lang.c++ Subject: Re: overloading array indexing Keywords: [] bugs? array indexing Message-ID: <10289@ulysses.homer.nj.att.com> Date: 15 May 88 16:09:03 GMT References: <451@ethz.UUCP> Sender: netnews@ulysses.homer.nj.att.com Reply-To: jss@hector (Jerry Schwarz) Organization: AT&T Bell Labs, Murray Hill Lines: 34 In article <451@ethz.UUCP> peter@ethz.UUCP writes: > >I have been trying to overload the array indexing operation as part of >a simulator I am writing. I am getting nowhere fast. > > The problem is that in the example subsrcipts a tiny*, rather than a tiny. Subscripting of pointers in C++ has the builtin meaning and never invokes a defined operator. The operator[] member is used when a value of the class type is subscripted. class tiny{ int v; public: tiny() {v=0;} void operator[](int i) { ... } // Used when subscripting a "tiny" }; main() { tiny fred[5]; // array of 5 tiny's fred[2]; // equivalent to *(&fred[0]+2), whose evaluation requires // no code. fred[2][99] ; // calls fred's subsrcipting operator, with this==&fred[2]. } Jerry Schwarz Bell Labs, Murray Hill