I am just learning function pointer and want to test how it works with member functions. The compilation of the following code fails at where it is marked.
# include <iostream>
# include <stdio.h>
using namespace std ;
class TMyClass {
public:
int DoIt ( float a, char b, char c ) {
cout << " TMyClass::DoIt " << endl ;
return a + b + c ;
}
int DoMore ( float a, char b, char c ) {
cout << " TMyClass::DoMore " << endl ;
return a - b + c ;
}
int ( TMyClass::*pt2Member ) ( float, char, char ) ;
int test_function_pointer ( ) {
this->pt2Member = &TMyClass::DoMore ;
int result = ( this -> *pt2Member ) ( 12, 'a', 'b' ) ; // wrong!
// expected unqualified-id before "*" token
return 0 ;
}
} ;
int main () {
TMyClass A ;
A.test_function_pointer () ;
return 0 ;
}
I wonder how to make it work. Thanks!