home *** CD-ROM | disk | FTP | other *** search
- Path: sparky!uunet!cs.utexas.edu!ut-emx!jamshid
- From: jamshid@ut-emx.uucp (Jamshid Afshar)
- Newsgroups: comp.lang.c++
- Subject: Re: Pointers to member functions
- Summary: FAQ Part17, get CPL2
- Message-ID: <77593@ut-emx.uucp>
- Date: 12 Aug 92 23:58:24 GMT
- References: <1992Aug9.230153.16147@organpipe.uug.arizona.edu>
- Organization: The University of Texas at Austin; Austin, Texas
- Lines: 70
-
- In article <1992Aug9.230153.16147@organpipe.uug.arizona.edu> dave@cs.arizona.edu (Dave Schaumann) writes:
- >[...]
- >While this behavior does make sense (you shouldn't be able to call a
- >member function as if it were a "normal" function), it also means that class
- >member functions behave in a way quite different from "normal" functions.
-
- Exactly!
-
- > class Foo {
- > public:
- > void print();
- > ...
- > };
- >[...]
- > 1. Just what is the type of the expression "foo.print"?
- > (as in the above context)
-
- I guess you could say its type is "member function pointer", but C++
- is very strict on the syntax for m.f.p.'s. Let's just say that the
- type of the expression "&Foo::print" is "void (Foo::*)(void)" -- a
- member function pointer.
-
- void (Foo::*mfp)(void); // 'mfp' is a pointer to a Foo member function
- // that returns 'void' and takes no params
- mfp = foo.print; // syntax error
- mfp = &foo.print; // syntax error
- mfp = Foo::print; // syntax error
- mfp = &Foo::print; // correct
-
- As you realized, you cannot invoke a member function pointer as you
- would invoke a regular function pointer. You must call it "on" an
- object. The syntax is:
-
- Foo* fooptr;
- Foo foo;
- ...
- (fooptr->*mfp)(); // call the member func pointed to by mfp on fooptr
- (foo.*mfp)(); // call the member func pointed to by mfp on foo
-
- Note, the extra set of parens are necessary because of precedence
- rules. Also note that you must always use the "->*" or ".*" operators
- when invoking a member function pointer, even inside a member function
- ('this' is *not* assumed).
-
- void Foo::call() {
- void (Foo::*mfp)(void) = ...;
- mfp(); // syntax error (doesn't assume call 'mfp' on this)
- (this->*mfp)(); // correct
- Foo& self = *this;
- (self.*mfp); // also correct
- }
-
- See the comp.lang.c++ FAQ Part17 (ftp sun.soe.clarkson.edu,
- pub/C++/FAQ) for more information about member function pointers (btw,
- there's also data member pointers) and other, more important,
- features of C++ (like templates).
-
- > 2. I guess I really need to get a technical book on C++,
- > preferrably one that is written in an accessable way.
- > What's my best choice?
-
- Don't write C++ without Stroustrup's _C++ Prog. Lang. 2nd Ed_. It
- contains the reference part of the _Annotated C++ Ref. Manual_ (the
- ANSI C++ committee base document). It does not contain the ARM's
- commentary, which you'll want if you really want to know why certain
- language decisions were made or want more clarification on the
- nitty-gritty details of C++.
-
- Jamshid Afshar
- jamshid@emx.utexas.edu
-