4

I have a class say Method with multiple functions for solving PDE's with different methods similar to the add, subtract, multiply... functions below. Another function in the class (DoMath in this examples) calls the methods. The user can change the method type (add, subtract,...), so I would like to create a Method pointer array (method_pointer), where the method is selected by choosing an integer "method_type". I have a version in C where everything is in one file without being members of a class, where this works fine; however, when the pointer is a member of the class I get "Method_pointer is not a function or function pointer" errors. I can't get the syntax correct, any help would be greatly appreciated. Below is a sample program of what I'm trying to do.

Method.h Header

class Method{
public:
    Method();
    ~Method();

void add(int, int, int);
void subtract(int, int, int);
void multiply(int, int, int);
void divide(int, int, int);

void DoMath(int, int, int);

void set_method( const int method_number);

private:
   int method_type;
};

Method.cc Source

#include "method.h"

typedef void (*method_function)(int, int,int);

Method::Method(){
    method_type=2;
}

Method::~Method(){
}

void Method::set_method( int method_number){
    method_type=method_number;
}

void Method::add( int a, int b, int c){
    c=a+b;
}

void Method::subtract( int a, int b, int c){
    c=a+b;
}

void Method::multiply( int a, int b, int c){
    c=a+b;
}

void Method::divide( int a, int b, int c){
    c=a+b;
}

void Method::DoMath(int x, int y, int result){
   method_function method_pointer[4]={add,subtract, multiply,divide};
   result=method_pointer[method_type](x,y);
}

main.cc

int main(int argc, const char * argv[]) {
    Method *method;
    int x=2;
    int y=3;
    int result=0;
    Method.DoMath(x,y,result);
    return 0;
}
3
  • Method.DoMath(x,y,result); What should this actually do? DoMath() is a non static member function, and the . dereferencing operator needs an instance not a type. Your code makes absolutely no sense. Commented Jul 29, 2015 at 18:37
  • possible duplicate of C++ function pointer (class member) to non-static member function Commented Jul 29, 2015 at 18:37
  • @xsquared In principle yes, but the OP's code seems to be seriously flawed from prior (trivial) mistakes. Commented Jul 29, 2015 at 18:40

1 Answer 1

11

A pointer to a method is a little bit different from a pointer to function. So, to declare one, you would write:

typedef void (Method::*method_function)(int, int,int);

and you would have to specify an object for which to call this method:

method_function method_pointer[4] = {&Method::add, &Method::subtract,  &Method::multiply, &Method::divide};
method_function func = method_pointer[method_type];
result = (this->*func)(x,y);

However, if you are going for your design, don't forget to make the third argument of a function a reference. Otherwise, you will not be able to extract the result. Thus, the proper typedef would be:

typedef void (Method::*method_function)(int, int,int&);

and you'd have to change your method signatures accordingly.

Finally, why are you using a pointer in your main function? You should probably use a stack variable (or at least initialise the pointer) and if you do, you need to call DoMath on the object instead of the class:

Method method;
// ...
method.DoMath(x, y, result);
Sign up to request clarification or add additional context in comments.

1 Comment

You just save my day and get me out of two days of debugging. Thank you a lot.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.