[comp.lang.c++] Pointers to functions anyone?

bernt@wolfen.cc.uow.oz (Bernt Ribbum) (09/24/90)

Hello Networld!

Anyone who could help a newcomer to C++?
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 <stream.h>

class test {
  void f1(int i)			// function to be called
    { cout<<"ok"<<i<<"\n"; }

  void (test::*ptf)(int);		// pointer-to-function

B
public:
  test() { ptf=test::f1; }		// constructor - assigns &f1 to ptf
  void execute (int i)
    { (*ptf)(i); }			// try to call f1()
};

main() {
  test variable;
  variable.execute(1);
}

My compiler (Turbo C++ V1.0) complains about the call (*ptf)(i):

-- Temporary used for parameter 'this' in function test::execute(int)
-- Type mismatch in parameter 'this' in function test::execute(int)
-- Parameter 'i' is never used in function test::execute(int)

What am I doing wrong?? It is a perfectly legal setup outside of classes
in any case! Or maybe I should use another way to do this?

Please email replies - I'll summarise here if someone has a solution!
Thanks,
-- 
  _
 /_) _  _  _  _/  Bernt Ribbum, Dept of Elec & Comp Eng, Univ of Wollongong
/_) (-'/ '/ > (   PO.Box 1144, Wollongong NSW 2500, AUSTRALIA  ------------
----------------  bernt@wolfen.cc.uow.edu.au  ----------------------------- 

landauer@morocco.Eng.Sun.COM (Doug Landauer) (09/30/90)

> Please email replies - I'll summarise here if someone has a solution!

I tried to, but I got an error message back

<<< 554 <bernt@wolfen.cc.uow.oz>... Unknown domain : cc . uow . oz
554 <bernt@wolfen.cc.uow.oz>... Service unavailable

So, this time I'll just post it.

> 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 <stream.h>
> 
> class test {
>   void f1(int i)			// function to be called
>     { cout<<"ok"<<i<<"\n"; }
> 
>   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
>   void execute (int i)
>     { (*ptf)(i); }			// try to call f1()
> };
> 
> main() {
>   test variable;
>   variable.execute(1);
> }
> 
> My compiler (Turbo C++ V1.0) complains about the call (*ptf)(i):
> 
> -- Temporary used for parameter 'this' in function test::execute(int)
> -- Type mismatch in parameter 'this' in function test::execute(int)
> -- Parameter 'i' is never used in function test::execute(int)

cfront 2.1 shows a little more clearly what's happening:
	"bernt.cc", line XX: error:  pointer to member dereferenced

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."

wai@swift.cs.tcd.ie (10/02/90)

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 <stream.h>
>> 
>> class test {
>>   void f1(int i)			// function to be called
>>     { cout<<"ok"<<i<<"\n"; }
>> 
>>   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.