0

For some reason I have to call class method from another method of the same class via function pointer. This is and example code which illustrates my question:

class testClass 
{
....
private:
void method(int parameter) {
    .....
};
void callingMethod();
};
typedef  void (testClass::*classMethod)(int parameter);

void testClass::callingMethod() {
    classMethod method = &testClass::method;
    method(1);
}

I get compilation error

error C2064: term does not evaluate to a function taking 1 arguments.

What is a right method to do such calls?

3 Answers3

0

Check this:

class testClass 
{
     public:
     int method(int parameter) 
     {
        cout << "Hi";
        return 0;
     };

     void callingMethod();
};


int (testClass::*pt2Member)(int) = NULL;

void testClass::callingMethod()
{
     pt2Member = &testClass::method;
     (*this.*pt2Member)(12);
     //classMethod method = &testClass::method;
     //*method(1);
}

int main()
{
    testClass objTest;
    objTest.callingMethod();
    return 0;
}
Biraj Borah
  • 101
  • 8
0

Your syntax is correct except that instead of

method(1);

you should use

(this->*method)(1);

or

(*this.*method)(1);
Beta
  • 96,650
  • 16
  • 149
  • 150
  • Thank you. My real case has a table of method pointers, do member method pointer, which get value form the table do the job. – user3013778 Jan 02 '14 at 07:20
0

It should be simply

(this->*method)(10);
Digital_Reality
  • 4,488
  • 1
  • 29
  • 31