home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1992 #16 / NN_1992_16.iso / spool / comp / sys / sgi / 11561 < prev    next >
Encoding:
Text File  |  1992-07-28  |  2.8 KB  |  88 lines

  1. Newsgroups: comp.sys.sgi
  2. Path: sparky!uunet!stanford.edu!CSD-NewsHost.Stanford.EDU!news
  3. From: philip@ziggy.stanford.edu (Philip Machanick)
  4. Subject: Re: C++ member pointer error
  5. Message-ID: <1992Jul29.030651.5087@CSD-NewsHost.Stanford.EDU>
  6. Sender: news@CSD-NewsHost.Stanford.EDU
  7. Reply-To: philip@ziggy.stanford.edu (Philip Machanick)
  8. Organization: CS Department, Stanford University, California, USA
  9. References: <1992Jul28.184745.2854@babbage.ece.uc.edu>
  10. Date: Wed, 29 Jul 1992 03:06:51 GMT
  11. Lines: 75
  12.  
  13. In article <1992Jul28.184745.2854@babbage.ece.uc.edu> tmcbraye@snert.ece.uc.edu  
  14. (Tim McBrayer) writes:
  15. > I'm trying to initialize a pointer to a class method and then call a function
  16. > through said pointer.  I'm working in C++ on a SGI IRIS.  As far as I can
  17. > tell, I'm accessing the pointer properly, according to Stroustrup's book.
  18. > However, when I compile with "CC junk.cc", I get:
  19. > "junk.cc", line 21: error:  object missing in call through pointer to
  20. > member function
  21. > The most frustrating part of this is that the code works under g++ on a 
  22. > Sparcstation.  Are there any known bugs with SGI C++ with IRIX 4.0.1? Does
  23. > anyone have any idea what 'object' is being referred to in the error
  24. > message?
  25.  
  26. I don't think this is a bug. I had the same results on a DECstation with cfront  
  27. ported directly from AT&T source. A call to a member function (unless it is
  28. declared static) must be made through an object of the class _where the
  29. member function is defined_. Although it appears you are calling the function  
  30. from an object of the appropriate class, this is not in fact so because under  
  31. other circumstances e.fn () may be called from a different place or even be  
  32. called after the original object is destroyed. (Do you think the semantics of  
  33. the line
  34.  
  35.    e.fn = (fnptr) &V::print;
  36.  
  37. should depend on the fact that this has happened inside a member function of  
  38. class V?)
  39.  
  40. If you want to ask a language lawyer for a more definitive answer post to  
  41. comp.lang.c++.
  42.  
  43. See modifications below for syntax (I found this in first edition of Lippman's  
  44. C++ primer p. 214) - I don't usually use this feature:
  45.  
  46. > #include <iostream.h>
  47. > class V{
  48. > public:
  49. >   int i;
  50. >   V() { i = 0;};
  51. >   void run();
  52. >   char * print(int x) {cout << "x is " << x << endl; return "Foo!";};
  53. > };
  54. > struct E {
  55. >   typedef char * (V::*fnptr)(int);
  56. >   fnptr fn;
  57. >   E() { fn = 0;};
  58. > };
  59. > void V::run() {
  60. >   E e;
  61. >   e.fn = (fnptr) &V::print;
  62. >   i = 42;
  63. >   cout << (e.fn)(i) << endl;  //ERROR: (*(e.fn))(i) gives the same error
  64. // this doesn't work because a non-static member function needs an object
  65. // of its own class (the "this" pointer)
  66.  
  67. // replace the above by:
  68.   cout << (this->*(e.fn))(i) << endl;
  69. > };
  70. > int main(){
  71. >   V v;
  72. >   v.run();
  73. >   cout << v.print(5) << endl;
  74. > }
  75. output:
  76.   x is 42
  77.   Foo!
  78.   x is 5
  79.   Foo!
  80. --
  81. Philip Machanick
  82. philip@pescadero.stanford.edu
  83.