Path: utzoo!utgpu!news-server.csri.toronto.edu!cs.utexas.edu!uunet!mcsun!ukc!tcdcs!swift.cs.tcd.ie!wai From: wai@swift.cs.tcd.ie Newsgroups: comp.lang.c++ Subject: Re: Pointers to functions anyone? Message-ID: <7120.2708a9e8@swift.cs.tcd.ie> Date: 2 Oct 90 14:53:28 GMT References: <6472@wolfen.cc.uow.oz> <1069@exodus.Eng.Sun.COM> Distribution: comp Organization: Computer Science Department, Trinity College Dublin Lines: 54 In article <1069@exodus.Eng.Sun.COM>, landauer@morocco.Eng.Sun.COM (Doug Landauer) writes: >> I'm trying to set up an array of pointers to functions within a class, >> much like the example here (for simplicity no array here): >> >> #include >> >> class test { >> void f1(int i) // function to be called >> { cout<<"ok"<> >> void (test::*ptf)(int); // pointer-to-function > > Whoops, your comment shows what's missing here. > ptf is *not* a "pointer-to-function". Instead, > it is a "pointer-to-MEMBER-function", which is > a different beast altogether. >> >> public: >> test() { ptf=test::f1; } // constructor - assigns &f1 to ptf Is there any difference between { ptf = &test::f1; } Both of them work okay. > Read the E&S ARM section about pointers to members, and your compiler > should be much happier about compiling your code. In order to make > your little test acceptable to cfront, all I had to do was change the > call to give the ptr-to-member-fn an explicit "this" for which to call > the function that it pointed at. Like so: > > Change the line > >> { (*ptf)(i); } // try to call f1() > > to a line like > >> { (this->*ptf)(i); } // try to call f1() > >> What am I doing wrong?? > > All pointers-to-members must be given an object for which to call > their pointed-at functions. Even if the call is from within another > member function. Ptrs-to-members are usually implemented as > OFFSETs within the class, rather than as true pointers. > > I hope this helps.... > -- > Doug Landauer - Sun Microsystems, Inc. - Languages - landauer@eng.sun.com > Matt Groening on C++: "Our language is one great salad." Is there any way to creat a generic pointer to member function class and yet be able to avoid all the signature type checking stuff? Wai.