home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1992 #18 / NN_1992_18.iso / spool / comp / lang / cplus / 12283 < prev    next >
Encoding:
Internet Message Format  |  1992-08-12  |  2.9 KB

  1. Path: sparky!uunet!cs.utexas.edu!ut-emx!jamshid
  2. From: jamshid@ut-emx.uucp (Jamshid Afshar)
  3. Newsgroups: comp.lang.c++
  4. Subject: Re: Pointers to member functions
  5. Summary: FAQ Part17, get CPL2
  6. Message-ID: <77593@ut-emx.uucp>
  7. Date: 12 Aug 92 23:58:24 GMT
  8. References: <1992Aug9.230153.16147@organpipe.uug.arizona.edu>
  9. Organization: The University of Texas at Austin; Austin, Texas
  10. Lines: 70
  11.  
  12. In article <1992Aug9.230153.16147@organpipe.uug.arizona.edu> dave@cs.arizona.edu (Dave Schaumann) writes:
  13. >[...]
  14. >While this behavior does make sense (you shouldn't be able to call a
  15. >member function as if it were a "normal" function), it also means that class
  16. >member functions behave in a way quite different from "normal" functions.
  17.  
  18. Exactly!
  19.  
  20. >    class Foo {
  21. >    public:
  22. >       void print();
  23. >       ...
  24. >    };
  25. >[...]
  26. >    1. Just what is the type of the expression "foo.print"?
  27. >       (as in the above context)
  28.  
  29. I guess you could say its type is "member function pointer", but C++
  30. is very strict on the syntax for m.f.p.'s.  Let's just say that the
  31. type of the expression "&Foo::print" is "void (Foo::*)(void)" -- a
  32. member function pointer.
  33.  
  34.     void (Foo::*mfp)(void); // 'mfp' is a pointer to a Foo member function
  35.                             // that returns 'void' and takes no params
  36.     mfp = foo.print;    // syntax error
  37.     mfp = &foo.print;   // syntax error
  38.     mfp = Foo::print;   // syntax error
  39.     mfp = &Foo::print;  // correct
  40.     
  41. As you realized, you cannot invoke a member function pointer as you
  42. would invoke a regular function pointer.  You must call it "on" an
  43. object.  The syntax is:
  44.  
  45.     Foo* fooptr;
  46.     Foo foo;
  47.     ...
  48.     (fooptr->*mfp)();  // call the member func pointed to by mfp on fooptr
  49.     (foo.*mfp)();  // call the member func pointed to by mfp on foo
  50.  
  51. Note, the extra set of parens are necessary because of precedence
  52. rules.  Also note that you must always use the "->*" or ".*" operators
  53. when invoking a member function pointer, even inside a member function
  54. ('this' is *not* assumed).
  55.  
  56.     void Foo::call() {
  57.        void (Foo::*mfp)(void) = ...;
  58.        mfp();           // syntax error (doesn't assume call 'mfp' on this)
  59.        (this->*mfp)();  // correct
  60.        Foo& self = *this;
  61.        (self.*mfp);     // also correct
  62.        }
  63.  
  64. See the comp.lang.c++ FAQ Part17 (ftp sun.soe.clarkson.edu,
  65. pub/C++/FAQ) for more information about member function pointers (btw,
  66. there's also data member pointers) and other, more important,
  67. features of C++ (like templates).
  68.  
  69. >    2. I guess I really need to get a technical book on C++,
  70. >       preferrably one that is written in an accessable way.
  71. >       What's my best choice?
  72.  
  73. Don't write C++ without Stroustrup's _C++ Prog. Lang. 2nd Ed_.  It
  74. contains the reference part of the _Annotated C++ Ref. Manual_ (the
  75. ANSI C++ committee base document).  It does not contain the ARM's
  76. commentary, which you'll want if you really want to know why certain
  77. language decisions were made or want more clarification on the
  78. nitty-gritty details of C++.
  79.  
  80. Jamshid Afshar
  81. jamshid@emx.utexas.edu
  82.