Path: utzoo!utgpu!news-server.csri.toronto.edu!rpi!crdgw1!uunet!huxley!glenn From: glenn@bitstream.com (Glenn P. Parker) Newsgroups: comp.lang.c++ Subject: Re: cast of a bound pointer: why ? Message-ID: Date: 13 Jun 91 15:17:53 GMT References: <1991Jun11.191016.9873@beaver.cs.washington.edu> <763@taumet.com> <1991Jun12.180414.16718@beaver.cs.washington.edu> <1991Jun12.192101.20607@Think.COM> Sender: glenn@huxley.UUCP Reply-To: (Glenn Parker) Distribution: comp Organization: Bitstream, Inc. Lines: 51 In-reply-to: barmar@think.com's message of 12 Jun 91 19:21:01 GMT In article <1991Jun12.192101.20607@Think.COM> barmar@think.com (Barry Margolin) writes: > In article <1991Jun12.180414.16718@beaver.cs.washington.edu> pauld@stowe.cs.washington.edu (Paul Barton-Davis) writes: > >This is the heart of the matter. C++ assumes that EVERY function > >*requires* a "this" pointer. > > No, only member functions. Close. Only non-static member functions require a "this" pointer. > >Any ideas on how one would do this if it were not possible (as it > >currently is) to obtain the actual address of a member function ? > >I have a few ideas myself, but would like to see some others if there > >are any. It *is* possible to take the address of a static member function. > Any problem (except efficiency, maybe) in computer science can be solved by > adding enough levels of indirection. > > In this case, define a non-member function that takes the class object as a > regular argument and simply invokes the member function, and pass the > address of this non-member function. The non-member function would look > something like this: > > int nonmember_fn (my_class object, int arg1, char *arg2) { > return object.member_fn (arg1, arg2); > } Or, define a static member function, and let it do all the work. Like so: class foo { public: // Don't confuse the following "static" with file static linkage. static int bar(foo*, int arg1, char* arg2); private: int baz; }; int foo::bar(foo* self, int arg1, char* arg2) { // Use self->baz to access private data member. } The address of foo::bar is &foo::bar. -- Glenn P. Parker glenn@bitstream.com Bitstream, Inc. uunet!huxley!glenn 215 First Street BIX: parker Cambridge, MA 02142-1270