make function pointer in class dependent on initialized value

rinkert

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;
}
mediocrevegetable1

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.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Function Pointer - variable not initialized

Check if a pointer to function is initialized

Casual value of a not initialized pointer to pointer

How to efficiently make the function pointer in the java class?

Exception safety of std::function initialized by a function pointer

How a pointer to an object which is not initialized by an address of object assign value to data member of class?

C++ How to make function pointer to class method

Function dependent on the value in the rows in R

Keeping the value of a String initialized into a function

Can a function return a value that is initialized in it?

How can I make the height of a barplot dependent on the value of a function on the same plot?

Why can a pointer to class can change value via function call?

Lambda for function pointer of class

Pointer to class member function

pointer to a member function of a class

Pointer to function in class variable

function pointer with vector and class

Jquery function to make multiple dependent dropdown

Is it possible to make a context-dependent function?

How to make 1 css value dependent on another?

Is a local class dependent if declared within a function template?

union of value and function pointer

How to make variadic template class method take function pointer as argument with type derived from function template?

std::dynamic_pointer_cast successful call on non initialized class

Is member value in the class initialized when an object is created?

Retrieve default value of in-class initialized member

Pointer error - "using uninitialized value" even though I think it is initialized?

Default value of function parameter initialized by list initialization

Why does taking a member function pointer value requires class name qualification even from inside of the class?