Newsgroups: comp.lang.c++ Path: utzoo!utgpu!news-server.csri.toronto.edu!rpi!zaphod.mps.ohio-state.edu!sdd.hp.com!elroy.jpl.nasa.gov!decwrl!world!wmm From: wmm@world.std.com (William M Miller) Subject: Re: Overloaded a->b == (*a).b ? Message-ID: <1991Mar26.231402.29997@world.std.com> Organization: The World Public Access UNIX, Brookline, MA References: <1991Mar25.225725.8625@cpsc.ucalgary.ca> Date: Tue, 26 Mar 1991 23:14:02 GMT Lines: 34 gintera@cs-sun-fsa.cpsc.ucalgary.ca (Andrew Ginter) writes: > In section 13.4, ARM suggests: > > "For example, if more than one of the operators ->, *, and [] > are defined for a class V, one would expect v->m, (*v).m, and > v[-].m to have identical values for all objects v of class V." > > At the same time, we find in section 13.4.6: > > "An expression x->m is interpreted as (x.operator->())->m for > a class object x. It follows that operator->() must either > return a pointer to a class or an object of or a reference to > a class for which operator->() is defined." > > If operator-> returns a reference to a class for which operator -> is > defined, the processes of resolving the overloaded operator -> > continues recursively until something other than an object of or a > reference to a class with an operator-> is returned. > > This same is NOT true of an overloaded operator*(). It may therefore > be non-trivial for a programmer to maintain the a->b == (*a).b > equivalence. > > Have I missed something here? I don't know. The lesson to be learned here, I think, is that having operator->() return something other than a pointer to a class is a feature that should usually be avoided, especially if there is also an operator*() defined. I'm sure there must be times when it's useful to be able to return an object of a class with operator->() defined instead of a pointer to a class, but I've never had need to do so. -- William M. Miller, Glockenspiel, Ltd. wmm@world.std.com