Path: utzoo!utgpu!jarvis.csri.toronto.edu!clyde.concordia.ca!uunet!cs.utexas.edu!hellgate.utah.edu!helios.ee.lbl.gov!ux1.lbl.gov!beard From: beard@ux1.lbl.gov (Patrick C Beard) Newsgroups: comp.lang.c++ Subject: Re: overloading -> Message-ID: <4559@helios.ee.lbl.gov> Date: 4 Jan 90 18:34:34 GMT References: <10159@saturn.ucsc.edu> Sender: usenet@helios.ee.lbl.gov Reply-To: beard@ux1.lbl.gov (Patrick C Beard) Organization: Lawrence Berkeley Laboratory, Berkeley Lines: 59 X-Local-Date: 4 Jan 90 10:34:34 PST In article <10159@saturn.ucsc.edu> daniel@spica.ucsc.edu (Daniel Edelson) writes: #I have a container class for a pointer type. #Is there a way to make -> work correctly #w/o explicit coercion or using (*p).member? # #Quick example: # #struct object { # int a; #}; # #typedef object * Pobject; # #struct container { # Pobject p; # container(Pobject pp) : p(pp) { } # operator Pobject() { return p; } # object & operator * () { return *p; } #}; # #main() #{ # object o; # container p = &o; # # (*p).a // works but unnatural style # Pobject(p)->a // works but explicit coercion is ugly # p->a; // doesn't work #} The reason p->a doesn't work is that p is not a pointer to an object and you haven't defined a member function operator->() for class container. Define: Pobject container::operator->() { return p; } # #I would the user-defined conversion to take place #before the -> operator is applied, but that doesn't appear #to be what the language specifies. I've tried 3 distinct #compilers (cfront 2.0, oregon, g++) and they all behave the #same. # It doesn't make sense for the conversion to be done since, in your example, you aren't dereferencing "p" when you write p->; p isn't a pointer. So the compilers are doing the right thing. #Help is greatly appreciated, # #Daniel Edelson # #daniel@cis.ucsc.edu ------------------------------------------------------------------------------- - Patrick Beard, Macintosh Programmer (beard@lbl.gov) - - Berkeley Systems, Inc. "..............Good day!" - Paul Harvey - -------------------------------------------------------------------------------