Path: utzoo!utgpu!news-server.csri.toronto.edu!rpi!zaphod.mps.ohio-state.edu!wuarchive!uunet!taumet!steve From: steve@taumet.com (Stephen Clamage) Newsgroups: comp.lang.c++ Subject: Re: cast of a bound pointer: why ? Message-ID: <763@taumet.com> Date: 12 Jun 91 16:22:55 GMT References: <1991Jun11.191016.9873@beaver.cs.washington.edu> Organization: Taumetric Corporation, San Diego Lines: 39 pauld@cs.washington.edu (Paul Barton-Davis) writes: >My understanding of this position is that it really boils down to an >syntactic nicety: > ptr to member function != ptr to function >Can anyone explain the necessity for this distinction, and perhaps >more importantly, can anyone comment on the possibility that it will >no longer be possible to use this construct ? It is not a syntactic nicety, but a fundamental semantic issue. A non-static member function requires a "this" pointer, which is implicitly supplied by the compiler when you call it. That is, given class C { ... foo(); ... } c; when you write c.foo(); it really means something like this (with artificial syntax): C::foo(&c); If you take the address of C::foo, cast it to an ordinary function pointer, and call foo via that pointer, no "this" pointer is supplied. int (*fp)() = (int(*)())C::foo; fp(); // call C::foo with no "this" pointer Function C::foo presumably uses "this", and will fail. Now suppose foo() is a virtual function. The actual function to be called depends on the actual object it is called in conjuction with: C *p = ...; p->foo(); might call C::foo(), or another foo() in class C's hierarchy. Therefore, the pointer-to-member-function must carry with it enough data about the virtual function so that the right virtual function is called. A simple address is not sufficient. (There are further complications with multiple inheritance when adjustment to "this" is required.) -- Steve Clamage, TauMetric Corp, steve@taumet.com