Let's say I have a class foo, with a lot of functions that take no arguments and return SomeType:
Now I have a pointer to this type of funct :
Now comes the tough part, I want to return it:
pfn = foo::quux;
What do I write instead of the ??????
Pointers to members can baffle even experienced C++ programmers. There are, however, some syntactic clues that can be helpful. First, let's look at an example that returns a pointer to an ordinary function:
The above code snippet declares a function named ipifd that takes double and returns a pointer to a function that takes int and returns int.
Pointers to member functions are very similar to ordinary pointers to function, except that they have an additional qualified name before the asterisk. Thus, for a class named C, the following declaration:
declares a function ipmifd that takes a double and returns a pointer to a member function of class C which takes int and returns int.
Now back to your class. A function that takes no argument and returns a member to function of foo, which in turn takes no arguments and returns int, looks like this:
This is cumbersome and error prone. In general, you should avoid using such constructs directly because other programmers will have a really hard time understanding what this declaration means. To improve code readability, I recommend using a tydepef to hide the ugly syntax:
Now, defining a function that returns a PFN is trivial:
typedef int (foo::*PFN)();