Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Function Pointer Array to a Method within a Class

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;
}
like image 635
mll36 Avatar asked Mar 15 '23 06:03

mll36


1 Answers

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);
like image 178
Maksim Solovjov Avatar answered Apr 02 '23 14:04

Maksim Solovjov