Path: utzoo!utgpu!news-server.csri.toronto.edu!rpi!usc!samsung!uunet!world!wmm From: wmm@world.std.com (William M Miller) Newsgroups: comp.lang.c++ Subject: Re: overloading operator . and -> Message-ID: <1991Mar25.154013.4044@world.std.com> Date: 25 Mar 91 15:40:13 GMT References: <14.UUL1.3#8618@softrue.UUCP> <4608@lupine.NCD.COM> Organization: The World Public Access UNIX, Brookline, MA Lines: 49 rfg@NCD.COM (Ron Guilmette) writes: > As any good book about C will tell you, this: > > ptr->member > > is really just a convenient "shorthand notation" for this: > > (*ptr).member > > Rather than treating -> as the bit of "syntactic sugar" that it was > always meant to be, somebody (and I think I know who) got the idea > that it ought to take on a life of its own. > > So it was reborn. (Or should I say "born again"? :-) And all the > faithful now call it an "operator" (which it clearly isn't, any more > that my aunt Emma is an "operator"). > > Of course, it would be one thing if we only *called* -> an "operator", > but unfortunately the language (as now defined) further compounds > this sillyness by allowing "->" to be overloaded! I think the fundamental disagreement is over whether you ought to follow the implicit relationships among overloadable operators or not. In the language as currently defined, there is no relationship among operator+(), operator+=(), and operator++(), even though according to normal semantics you could easily implement both operator+() and operator++() in terms of operator+=(). If you don't supply one of these operators, it's simply undefined, not inferred from the ones you do supply. The reason this relates to "operator->()" is because, in the normal C decomposition "(*ptr).member", it's clear that the "*" _is_ an operator and (as I'm sure you would agree) rightly overloadable. The question is, should "ptr->member" invoke operator*() if "ptr" is an object of a class with operator*() defined? If you think so, then clearly there's no need for a separate operator->(). However, existing C++ practice is that you only invoke an overloaded operator if the operator itself appears explicitly in the text, ignoring the C identities. In this view, you _can't_ invoke operator*() for "ptr->member"; therefore, if you want to be able to overload the conceptual "*" in "ptr->member", you _have_ to allow operator->(). For me, allowing operator->() leads to a more consistent language while still permitting me the syntactic convenience of writing ptr->member instead of (*ptr).member. I agree that the expansion of "ptr->member" to "(ptr.operator->())->member" is a bit strange, but I think the alternatives (implicit invocation of operator*() for "->" or not allowing the ptr->member notation at all) are worse. -- William M. Miller, Glockenspiel, Ltd. wmm@world.std.com