home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1992 #19 / NN_1992_19.iso / spool / comp / lang / cplus / 13102 < prev    next >
Encoding:
Text File  |  1992-08-31  |  2.5 KB  |  98 lines

  1. Newsgroups: comp.lang.c++
  2. Path: sparky!uunet!zaphod.mps.ohio-state.edu!cs.utexas.edu!torn!watserv2.uwaterloo.ca!watmath!watcgl!bambam.uwaterloo.ca!bhickey
  3. From: bhickey@bambam.uwaterloo.ca (Bruce Hickey)
  4. Subject: Pointer to member function as parameter
  5. Message-ID: <Btv0xr.A6H@watcgl.uwaterloo.ca>
  6. Keywords: pointer, member function
  7. Sender: news@watcgl.uwaterloo.ca (USENET News System)
  8. Organization: Computer Graphics Laboratory, University of Waterloo, Ontario, Canada
  9. Date: Mon, 31 Aug 1992 18:05:51 GMT
  10. Lines: 86
  11.  
  12. Hi,
  13.  
  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.  
  18. I am trying to create a pointer to a member function as well as use that 
  19. pointer to call the function. The problem arises when I try to pass this 
  20. pointer as a paramter to a function. There appears to be no reason why this 
  21. is not possible. 
  22.  
  23. Compiling the attached code I get two errors:
  24.  
  25. 1) "test.cc", line 17: error:  object missing in call through pointer to member function - If this is incorrect how would I call the function?
  26.  
  27. 2) "test.cc", line 28: error: & .*  expression - Since I want to pass a pointer
  28. do I not need its address?
  29.  
  30. The second piece of code, which works, uses only global functions. The 
  31. extension of this code to include class member functions is unpleasantly 
  32. confusing.
  33.  
  34. Suggestions regarding the solutions or simply a correct example would be
  35. appreciated.
  36.  
  37. Thanks,
  38. Bruce Hickey
  39.  
  40. -------------------- Compiled with AT&T 3.0.1 ------------------------------
  41. #include <iostream.h>
  42.  
  43. class mine {
  44. public:
  45.     double func( int );
  46. };
  47.  
  48. double mine::func( int var1 ) 
  49.     cerr << var1 << "\n"; 
  50.     return 0.1;
  51. }
  52.  
  53. // This function wants a pointer to a member function.
  54. void freefunc( double (mine::*f)(int) ) 
  55.     cerr << "calling func\n";
  56.     (*f)(2);            // How do I call the function?
  57. }
  58.  
  59. int main()
  60. {
  61. mine test;        
  62.  
  63. double (mine::*pf)(int) = &mine::func;
  64.  
  65. (test.*pf)(5);         // Call the member function through pointer
  66.  
  67. freefunc(test.*pf);     // Attempt to pass pointer to function
  68.  
  69. }
  70.  
  71. -----------------------Working version with only global functions ------------
  72.  
  73. #include <iostream.h>
  74.  
  75. double func1( int  var) 
  76.     cerr << "func 1 " << var << "\n";
  77.     return 0.1;
  78. }
  79.  
  80. void freefunc1( double (*f)(int) ) 
  81.     cerr << "calling func1\n";
  82.     f(2);
  83. }
  84.  
  85. int main()
  86. {
  87. double (*pf1)(int) = func1;
  88.  
  89. (*pf1)(5);        // Call the function through the pointer
  90. freefunc1(*pf1);    // Pass the pointer to the fuction as a parameter
  91.  
  92. }
  93.  
  94.