Path: utzoo!attcan!uunet!cs.utexas.edu!tut.cis.ohio-state.edu!uc!nic.MR.NET!nic.stolaf.edu!news From: hannum@mahler.psu.edu (Charles Hannum) Newsgroups: comp.lang.c++ Subject: Re: problems with [] operator Message-ID: <1990Jul6.125741.28182@acc.stolaf.edu> Date: 6 Jul 90 12:57:41 GMT References: <27893@ethz-inf.UUCP> Sender: news@acc.stolaf.edu Organization: The Pennsylvania State University Public NeXT Laboratory Lines: 35 In-Reply-To: reberhar@ethz-inf's message of 28 Jun 90 15:07:57 GMT In article <27893@ethz-inf.UUCP> reberhar@ethz-inf (Rolf Georg Eberhardt) writes: ... In all C++ books I've looked into the []-operator must return a reference. ... You just answered your own question [B-)]. "Reference to T" and "pointer to T" are NOT the same. They are stored the same internally, but "reference to T" is treated exactly as if it were a "T", whereas "pointer to T" is distinct. For example, if one declares: struct foobar { int bar; } foo[20]; foo[] returns a foobar, not a foobar*. To demonstrate: foo[10]->bar = 0; // error: non-pointer -> bar (&foo[10])->bar = 0; // ok (In this case, we're forcing it to treat foo[] as a pointer by using the & operator.) This is what you really wanted to say before: foo[10].bar = 0; The reader can extrapolate this to the previous case. -- Virtually, Charles Martin Hannum "Those who say a thing cannot be done should Please send mail to: under no circumstances stand in the way of hannum@schubert.psu.edu he who is doing it." - a misquote