Search code examples
c++code-generation

make function pointer in class dependent on initialized value


I want to create an object, and during initialisation choose a function to perform some calculation. For a polynomial of order N, some function has to be called, defined as someFunN. Now I am able to do this with a function pointer. I do this by a huge if block in the constructor,

if (order == 2)
    SolveFun = &someFunPoly2;
else if (order == 3)
    SolveFun = &someFunPoly3;
// etc...
else
    SolveFun = &someFunPoly50;

but since I have functions till order ~50, this is a bit tedious to write. Is there any other way I can define someFunN and assign this function during initialisation of the Polynomial?

The contents of someFunN are generated by a code generation script in Matlab, and can still be changed.

#include <iostream>

using namespace std;
struct vec;
class Polynomial;
double someFunPoly2(Polynomial *Poly, vec Pt, vec Ur);
double someFunPoly3(Polynomial *Poly, vec Pt, vec Ur);
double someFunPoly10(Polynomial *Poly, vec Pt, vec Ur); 

struct vec
{
    double x, y;
    vec(double x_, double y_) : x(x_), y(y_) {}
};

class Polynomial
{
public:
    int order, n;
    double *p;
    double (*SolveFun)(Polynomial *, vec, vec);
    Polynomial(int order_)
    {
        order = order_;
        n = order + 1;
        p = new double[n];
        for (int i = 0; i < n; i++)
            p[i] = 0.0;

        if (order == 2)
            SolveFun = &someFunPoly2;
        else if (order == 3)
            SolveFun = &someFunPoly3;
        else
            SolveFun = &someFunPoly10;
        // more and more cases...
    }
};

double someFunPoly2(Polynomial *Poly, vec Pt, vec Ur)
{
    // some calculations for a poly of order 2
    cout << "using Poly with order " << Poly->order << " and this is someFunPoly2" << endl;
    return 2;
}

double someFunPoly3(Polynomial *Poly, vec Pt, vec Ur)
{
    // some calculations for a poly of order 2
    cout << "using Poly with order " << Poly->order << " and this is someFunPoly3" << endl;
    return 3;
}

double someFunPoly10(Polynomial *Poly, vec Pt, vec Ur)
{
    // some calculations for a poly of order 10
    cout << "using Poly with order " << Poly->order << " and this is someFunPoly10" << endl;
    return 10;
}

int main()
{
    vec P = vec(1.0, 2.0);
    vec U = vec(0.3, 0.5);
    Polynomial *Poly2 = new Polynomial(2);
    Polynomial *Poly10 = new Polynomial(10);

    cout << Poly2->SolveFun(Poly2, P, U) << endl;
    cout << Poly10->SolveFun(Poly10, P, U) << endl;

    return 0;
}

Solution

  • You're probably looking for a lookup table:

    #include <iostream>
    
    void say_hello()  {std::cout << "Hello!\n";}
    void say_bye()    {std::cout << "Bye!\n";}
    void say_thanks() {std::cout << "Thanks!\n";}
    
    int main(void)
    {
        int n = /*something 0-2*/;
        void (*says[])() = {say_hello, say_bye, say_thanks};
        void (*speech)() = says[n];
        speech();
    }
    

    If n is 0, 1 or 2, then it'll print Hello!, Bye! or Thanks! respectively.