C++ Function Pointers to Member Methods in Class


In this tutorial, we used the pointer to normal C++ function. The usage of normal function pointer looks simple to use. However, with Object Oriented programming concepts, the member methods defined within class are somewhat different. This example shows that the static methods defined in class are quite similar to normal functions.

Now, in order to spot the difference, we create a class which has a static method and a non-static method.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
class Integral {
public:
    Integral() {
 
    }
 
    static double f(double x) {
        return cos(x);
    }
 
    double g(double x) {
        return cos(x);
    }
}
class Integral {
public:
    Integral() {

    }

    static double f(double x) {
        return cos(x);
    }

    double g(double x) {
        return cos(x);
    }
}

Now, for static method f, the usage is similar to normal functions.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
class Integral {
    double integral(double(*f)(double x), double a, double b, int n) {
        double step = (b - a) / n;
        double area = 0.0;
        for (int i = 0; i < n; i ++) {
            area += f(a + (i + 0.5) * step) * step;
        }
        return area;
    }
}
// helloacm.com
int main() {
    Integral *obj = new Integral();
    cout << obj->integral(obj->f, 0, M_PI / 2, 100);
}
class Integral {
    double integral(double(*f)(double x), double a, double b, int n) {
        double step = (b - a) / n;
        double area = 0.0;
        for (int i = 0; i < n; i ++) {
            area += f(a + (i + 0.5) * step) * step;
        }
        return area;
    }
}
// helloacm.com
int main() {
    Integral *obj = new Integral();
    cout << obj->integral(obj->f, 0, M_PI / 2, 100);
}

The following also works:

1
2
3
4
int main() {
    Integral *obj = new Integral();
    cout << obj->integral(Integral::f, 0, M_PI / 2, 100);
}
int main() {
    Integral *obj = new Integral();
    cout << obj->integral(Integral::f, 0, M_PI / 2, 100);
}

It does not matter if the function integral is defined within or outside the class (normal function). The non-static method f can also be passed into functions as parameters by using Integral::f.

However, if the function is non-static, like the g() defined above. We need a tricky way for a workaround.

1
2
3
4
5
6
7
8
9
    double integral(double(Integral::*f)(double x), double a, double b, int n) {
        double step = (b - a) / n;
        double area = 0.0;
        for (int i = 0; i < n; i ++) {
            area += Integral::f(a + (i + 0.5) * step) * step;
        }
        return area;
    }
// helloacm.com
    double integral(double(Integral::*f)(double x), double a, double b, int n) {
        double step = (b - a) / n;
        double area = 0.0;
        for (int i = 0; i < n; i ++) {
            area += Integral::f(a + (i + 0.5) * step) * step;
        }
        return area;
    }
// helloacm.com

We need a type double(Integral::*f)(double x) to explicitly specify that it is a pointer to the member function of a class. Also, when we deference (invoke) the function pointer, we need to use Integral::f().

At the function calling, we use:

1
cout << obj->integral(&Integral::g, 0, M_PI / 2, 100);
cout << obj->integral(&Integral::g, 0, M_PI / 2, 100);

Please note that we need a & to explicitly say it is the address of the function while this is optional if it is a static function (i.e. address assumed, with or without the & sign)

If you don’t do it this way, e.g. The function pointer isn’t defined as double(Integral::*f)(double x), then the compiler will complain.

E:\Temp\Integral\main.cpp||In function 'int main()':|
E:\Temp\Integral\main.cpp|47|error: no matching function for call to 'Integral::integral(double (Integral::*)(double), int, double, int)'|
E:\Temp\Integral\main.cpp|47|note: candidate is:|
E:\Temp\Integral\main.cpp|20|note: double Integral::integral(double (*)(double), double, double, int)|
E:\Temp\Integral\main.cpp|20|note:   no known conversion for argument 1 from 'double (Integral::*)(double)' to 'double (*)(double)'|
||=== Build finished: 1 errors, 0 warnings (0 minutes, 0 seconds) ===|

–EOF (The Ultimate Computing & Technology Blog) —

GD Star Rating
loading...
600 words
Last Post: C++ Function to Compute Numerical Integral Using Function Pointers
Next Post: Enter the Command Shell Prompt with Specified Directory on Windows (Registry)

The Permanent URL is: C++ Function Pointers to Member Methods in Class

2 Comments

  1. YT

Leave a Reply