Download example 3 ex3.cpp - Using Class Member functions via pointers.
Using Class Member Function PointersLets pretend that static class functions don't exist for now as those haven't been covered in a tutorial yet. That leaves class member functions. In example two, the function MyFunc() was not part of a class. It existed in isolation. But realistically we may well want to call class member functions through a pointer.
Here is the problem. It doesn't make sense to just take the address of a class member function (which is what we did with MyFunc) because we don't know which instance of the class we mean. What if we had an array of ten objects. Which one would it use? It needs the object as well as the function.
Defining the variable to hold the function is more or less the same as before. The syntax though is a bit uglier because the class is used. First the variable is declared to hold the class function pointer.
This has to have the class prefix CList:: or else the compiler will treat it as a non class function pointer. Likewise, anywhere it is assigned- in this function
int (CList::*m_pointerToMember)(pElement) ;
also requires the same treatment and the function assigned in List.SetCallback must be specified though you have the choice of either
void SetCallback( int (CList::*newpointerToMember)(pElement)) ;
Either will do though my preference is the latter.
On the next page : A recap on the syntax of Function Pointers