home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1992 #19 / NN_1992_19.iso / spool / comp / lang / cplus / 13225 < prev    next >
Encoding:
Text File  |  1992-09-02  |  2.8 KB  |  108 lines

  1. Path: sparky!uunet!sun-barr!cs.utexas.edu!devnull!thunder.Berkeley.EDU!rjf
  2. From: rjf@thunder.Berkeley.EDU (Russell Fleming)
  3. Newsgroups: comp.lang.c++
  4. Subject: Re: Pointer to member function as parameter
  5. Keywords: pointer, member function
  6. Message-ID: <2366@devnull.mpd.tandem.com>
  7. Date: 2 Sep 92 19:05:41 GMT
  8. References: <Btv0xr.A6H@watcgl.uwaterloo.ca>
  9. Sender: news@devnull.mpd.tandem.com
  10. Lines: 96
  11.  
  12. In article <Btv0xr.A6H@watcgl.uwaterloo.ca>, bhickey@bambam.uwaterloo.ca
  13. (Bruce Hickey) writes:
  14. > I am attempting to pass a pointer to a member function as an argument to
  15. > a function. Neither Stroustrup (2nd Ed) or Lippman (2nd Ed) cover this
  16. > subject directly, to my knowledge. 
  17. > I am trying to create a pointer to a member function as well as use that 
  18. > pointer to call the function. The problem arises when I try to pass this 
  19. > pointer as a paramter to a function. There appears to be no reason why this 
  20. > is not possible. 
  21. >
  22. > class mine { 
  23. > public:
  24. >     double func( int );
  25. > };
  26. > double mine::func( int var1 ) 
  27. > { 
  28. >     cerr << var1 << "\n"; 
  29. >     return 0.1;
  30. > }
  31. > // This function wants a pointer to a member function.
  32. > void freefunc( double (mine::*f)(int) ) 
  33. > { 
  34. >     cerr << "calling func\n";
  35. >     (*f)(2);            // How do I call the function?
  36. > }
  37. > int main()
  38. > {
  39. > mine test;        
  40. > double (mine::*pf)(int) = &mine::func;
  41. > (test.*pf)(5);         // Call the member function through pointer
  42. > freefunc(test.*pf);     // Attempt to pass pointer to function
  43. > }
  44.  
  45. There is nothing wrong with your declaration of a pointer to the member
  46. function.  The problem lies with your use of the pointer.  All
  47. non-static member functions have an implied "this" argument which is
  48. used to access the data associated with the object.  When you use a
  49. ptr-to-mem-fcn to invoke the function, you must also provide the object
  50. which the function is to act upon (ie. (this->*f)(2)).  Two solutions to
  51. your problem are available.  The first elimintates the need for a
  52. pointer.  Simply modify your code as follows:
  53.  
  54. void freefunc (mine& m)
  55. {
  56.     cerr << "calling func\n";
  57.     m.func (2);
  58. }
  59.  
  60. int main ()
  61. {
  62.     mine test;
  63.     freefunc (test);
  64. }
  65.  
  66. Alternatively, since the member function you are using does not (as
  67. shown) rely on any data from the object,
  68. you could re-write your code to use a pointer function as follows:
  69.  
  70. class mine {
  71. public:
  72.     static double func(int);
  73.     };
  74.  
  75. double mine::func (int var1)
  76. {
  77.     cerr << var1 << "\n";
  78.     return 0.1;
  79. }
  80.  
  81. void freefunc (double (*f)(int))
  82. {
  83.     cerr << "calling func\n";
  84.     (*f)(2);
  85. }
  86.  
  87. int main ()
  88. {
  89.     mine test;
  90.     double (*pf)(int) = &mine::func;
  91.     freefunc (pf);
  92. }
  93.  
  94. =================================================================
  95. Rusty Fleming, Software Consultant @ Tandem Computers Inc.
  96. email: rjf@mpd.tandem.com            Austin, Texas
  97. voice: (512) 244 - 8390              USA
  98.