Path: utzoo!news-server.csri.toronto.edu!torsqnt!hybrid!scifi!bywater!uunet!microsoft!jimad From: jimad@microsoft.UUCP (Jim ADCOCK) Newsgroups: comp.lang.c++ Subject: Re: overloading operator . and -> Message-ID: <71328@microsoft.UUCP> Date: 15 Mar 91 19:13:50 GMT References: <14.UUL1.3#8618@softrue.UUCP> Reply-To: jimad@microsoft.UUCP (Jim ADCOCK) Organization: Microsoft Corp., Redmond WA Lines: 79 In article <14.UUL1.3#8618@softrue.UUCP> kearns@softrue.UUCP (Steven Kearns) writes: >Really, we should be able to overload not operator ->, but >operator ->f and operator->g(int x) etc... In other words, the >real operation being applied to X in X->f is "arrow f". > >This provides more flexibility than the current system because you >are not restricted to eventually returning something for which ->f >makes sense. It also seems more intuitive to me. Your suggestion does not provide more flexibility than the current system because the current system allows one do what you are asking for -- albeit not exactly with the syntax you are requesting: ----- extern "C" void printf(const char*, ...); // probably ought to be nested: class Xs_arrow_operators { public: void f() { printf("function X:: ->f()\n"); } void g(int i) { printf("function X:: ->g(int i=%d)\n", i); } }; class X { Xs_arrow_operators arrow; public: Xs_arrow_operators* operator->() { return &arrow; } }; main() { X x; x->f(); x->g(5); return 0; } ---- Thus, by introducing a dummy helper class [which probably ought to be nested] to serve as a target for the current operator->, you can then overload each method in that dummy class as you like. Likewise, if the ANSI-C++ committee chooses to also allow overloaded operator dot, then you'll also be able to meet your needs with operator dot. However, going the other way, if C++ were to require individual overloading of each method to be retargeted, then programmers would have to manually [ or via hack ] implement a forwarding method for each method in order to be able to create a smart pointer or smart reference classes. IE O(N) extra programmer typing -- where N is the number of methods requiring forwarding. This would prevent generally-reusable template versions of smart pointer and smart reference classes -- not to mention make such onerous to program. Whereas, going the other way, having to introduce an intermediate dummy helper class only requires O(1) extra programmer typing. A more interesting question [to my mind] is whether C++ should [have] also allow[ed]: void operator->(class DispatchStyle) // likewise operator dot This would allow overriding of the method of dispatch: void X::operator->(class HashDispatchedMethod method) { hashDispatch(method); } As of today, one can either override the dispatch technique for all members of a class [albeit with grave restrictions] or you can individually override the dispatch for each member individually [via an inline function] but there's no way to break the members of a class into separate categories, where each category uses a different dispatch technique. [please note I am not requesting such functionality -- I'm only mentioning it as a curiousity.]