From -6122140294346352249 X-Google-Language: ENGLISH,ASCII-7-bit X-Google-Thread: f78e5,18c6d861cef54158 X-Google-Attributes: gidf78e5,public X-Google-ArrivalTime: 2003-01-15 10:28:03 PST Path: archiver1.google.com!news1.google.com!newsfeed.stanford.edu!skynet.be!skynet.be!newsfeed.icl.net!newsfeed.fjserv.net!kibo.news.demon.net!mutlu.news.demon.net!demon!mail2news.demon.co.uk!devnull From: danielgutson@hotmail.com (danielgutson@hotmail.com) Newsgroups: comp.std.c++ Subject: Re: automatic downcast support through function parameters Date: Wed, 15 Jan 2003 18:27:58 +0000 (UTC) Organization: http://groups.google.com/ Lines: 114 Approved: fjh@cs.mu.oz.au (Fergus Henderson , moderator of comp.std.c++) Message-ID: <23478c42.0301150659.1eefb5e0@posting.google.com> References: <23478c42.0212310830.23dbc580@posting.google.com> <3E120608.6060608@tellabs.com> <23478c42.0301020923.79359e66@posting.google.com> <7f2735a5.0301071313.88d2480@posting.google.com> Content-Type: text/plain; charset=ISO-8859-1 Content-Transfer-Encoding: 8bit X-Trace: mail2news.demon.co.uk 1042655278 3348 10.0.0.1 (15 Jan 2003 18:27:58 GMT) X-Complaints-To: abuse@demon.net NNTP-Posting-Date: Wed, 15 Jan 2003 18:27:58 +0000 (UTC) X-Received: from mulga.cs.mu.oz.au ([128.250.1.22]) by news.demon.co.uk with esmtp (Exim 4.05) id 18YsGb-0000rr-00 for mail2news@news.news.demon.net; Wed, 15 Jan 2003 18:27:57 +0000 X-Received: from localhost (localhost [[UNIX: localhost]]) by mulga.cs.mu.OZ.AU id FAA22996; Thu, 16 Jan 2003 05:27:46 +1100 (EST) X-Authentication-Warning: mulga.cs.mu.OZ.AU: fjh set sender to devnull@stump.algebra.com using -f X-Path: comp-std-cpp-robomod!not-for-mail X-Robomod: STUMP, ichudov@algebra.com (Igor Chudov) X-Delivered-To: std-c++@ncar.ucar.edu X-Newsgroups: comp.std.c++ X-NNTP-Posting-Date: 15 Jan 2003 14:59:15 GMT X-Spam-Status: No, hits=-8.4 required=5.0 tests=FORGED_HOTMAIL_RCVD,NOSPAM_INC,QUOTED_EMAIL_TEXT, REFERENCES,SPAM_PHRASE_03_05 version=2.41 Xref: archiver1.google.com comp.std.c++:17120 allan_w@my-dejanews.com (Allan W) wrote in message news:<7f2735a5.0301071313.88d2480@posting.google.com>... Hello Alan: First, I'm afraid that virtual functions can only be member functions, as far as there is a 'virtual table OF a class' (there are no 'global' virtual tables). (I strongly suggest to compile or test your code before posting). Second, I'm not trying to do anything similar with virtual functions, just hold abstraction of the received parameter: a function receives an 'upcasted' parameter (contravariant), ignoring the original derived class, and simply recover it when used. As many people posted in this thread, RTTI is needed. They are also right about compiling units: the linker should solve this. Finally, the Visitor pattern does not apply to this situation. regards, Daniel. > > > danielgutson@hotmail.com wrote: > > > > People: I'd like to ask about the feasibility of this: > > > > automatic downcast during function calls. > > > > let's have: > > > > struct Base{}; > > > > struct Der : Base{}; > > > > > > > > void g(Base* b); > > > > void g(Der* d); > > > > > > > > void f(upcasted Base* b) // see below > > > > { > > > > g(b); > > > > } > > > > > > > > void main(void) > > > > { > > > > Der d; > > > > f(&d); > > > > } > > > > > > > > - The idea here is that f ignores the function that will be finally > > > > invoked, but knows that the pointer was upcasted ('upcasted' > > > > keyword). > > > > Then, the function accepting the most derived class (closest to the > > > > original type) will be invoked, in this case: g(Der*). > > > > > > > > The 'spirit' of this is to recover specificity: in the process flow: > > > > main -> f -> g > > > > specificity(main) = specificity(g) > specificity(f) > > > > and this would be an automatic way of recoviring specificity at g, > > > > without loosing generality at f. > > > > > > > > What you think? > > > Daniel.Miller@tellabs.com ("Dan'l Miller") wrote > something about global flow analysis, which misses the point. > > > I'm talking ... a kind of condition using RTTI. > ... > > can be achieved using a reduced set of RTTI primitives. I think, that > ... > > just with some additional entry in the v-table. > > Basically you want to do with free functions, the same thing that is > done with virtual functions. I think another thing that would accomplish > what you want is to move virtual functions out of the class definition: > > struct Base {}; > struct Der : public Base {}; > virtual void g(Base*) { std::cout << "g(Base*)"; } > virtual void g(Der*) { std::cout << "g(Der *)"; } > void callg(Base*b) { g(b); } > int main() { > Base b; callg(&b); // Displays "g(Base*)" > Der d; callg(&d); // Displays "g(Der *)" > } > > Consider refactoring your code. If possible, put a member function in > Base and Der that simply call g() as appropriate. Works today, without > any changes to the language. > > struct Base { > virtual void callg() { g(this); } > }; > struct Der : public Base { > virtual void callg() { g(this); } > }; > virtual void g(Base*) { std::cout << "g(Base*)"; } > virtual void g(Der*) { std::cout << "g(Der *)"; } > void callg(Base*b) { b->callg(); } > int main() { > Base b; callg(&b); // Displays "g(Base*)" > Der d; callg(&d); // Displays "g(Der *)" > } > > You might also want to examine the Visitor pattern for a more > general-purpose solution to this same problem. > > --- > [ comp.std.c++ is moderated. To submit articles, try just posting with ] > [ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ] > [ --- Please see the FAQ before posting. --- ] > [ FAQ: http://www.jamesd.demon.co.uk/csc/faq.html ] --- [ comp.std.c++ is moderated. To submit articles, try just posting with ] [ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ] [ --- Please see the FAQ before posting. --- ] [ FAQ: http://www.jamesd.demon.co.uk/csc/faq.html ]