Yep.
Consider the following function:
The type of this function is different depending on whether it is an ordinary function or a non-static member function of some class:
Note: if it's a static member function of class Fred, its type is the same as if it was an ordinary function: "int (*)(char,float)".
[ Top | Bottom | Previous section | Next section | Search the FAQ ]
Don't.
Because a member function is meaningless without an object to invoke it on, you can't do this directly (if The X Windows System was rewritten in C++, it would probably pass references to objects around, not just pointers to functions; naturally the objects would embody the required function and probably a whole lot more).
As a patch for existing software, use a top-level (non-member) function as a wrapper which takes an object obtained through some other technique (held in a global, perhaps). The top-level function would apply the desired member function against the global object.
E.g., suppose you want to call Fred::memberFunction() on interrupt:
Note: static member functions do not require an actual object to be invoked, so pointers-to-static-member-functions are type compatible with regular pointers-to-functions.
[ Top | Bottom | Previous section | Next section | Search the FAQ ]
This is a special case of the previous two questions, therefore read the previous two answers first.
Non-static member functions have a hidden parameter that corresponds to the this pointer. The this pointer points to the instance data for the object. The interrupt hardware/firmware in the system is not capable of providing the this pointer argument. You must use "normal" functions (non class members) or static member functions as interrupt service routines.
One possible solution is to use a static member as the interrupt service routine and have that function look somewhere to find the instance/member pair that should be called on interrupt. Thus the effect is that a member function is invoked on an interrupt, but for technical reasons you need to call an intermediate function first.
[ Top | Bottom | Previous section | Next section | Search the FAQ ]
This is a corollary to the previous FAQ.
Long answer: In C++, member functions have an implicit parameter which points to the object (the this pointer inside the member function). Normal C functions can be thought of as having a different calling convention from member functions, so the types of their pointers (pointer-to-member-function vs. pointer-to-function) are different and incompatible. C++ introduces a new type of pointer, called a pointer-to-member, which can be invoked only by providing an object.
NOTE: do not attempt to "cast" a pointer-to-member-function into a pointer-to-function; the result is undefined and probably disastrous. E.g., a pointer-to-member-function is not required to contain the machine address of the appropriate function. As was said in the last example, if you have a pointer to a regular C function, use either a top-level (non-member) function, or a static (class) member function.
[ Top | Bottom | Previous section | Next section | Search the FAQ ]
Two things: (1) use a typedef, and (2) use a #define macro.
Here's the way you create the typedef:
Here's the way you create the #define macro (normally I dislike #define macros, but this is one of those rare cases where they actually improve the readability and writability of your code):
Here's how you use these features:
I strongly recommend these features. In the real world, member function invocations are a lot more complex than the simple example just given, and the difference in readability and writability is significant. comp.lang.c++ has had to endure hundreds and hundreds of postings from confused programmers who couldn't quite get the syntax right. Almost all these errors would have vanished had they used these features.
Note: #define macros are evil in 4 different ways: evil#1, evil#2, evil#3, and evil#4. But they're still useful sometimes. But you should still feel a vague sense of shame after using them.
[ Top | Bottom | Previous section | Next section | Search the FAQ ]
Use the usual typedef and #define macro and you're 90% done.
First, use a typedef:
That makes the array of pointers-to-member-functions straightforward:
Second, use the callMemberFunction macro:
That makes calling one of the member functions on object "fred" straightforward:
Note: #define macros are evil in 4 different ways: evil#1, evil#2, evil#3, and evil#4. But they're still useful sometimes. Feel ashamed, feel guilty, but when an evil construct like a macro improves your software, use it.
[ Top | Bottom | Previous section | Next section | Search the FAQ ]
E-mail the author
[ C++ FAQ Lite
| Table of contents
| Subject index
| About the author
| ©
| Download your own copy ]
Revised Aug 15, 2001