Function pointer workaround?

Hi, I'm working with some software that doesn't support function pointers. But I need them for a project I'm working on. Does anyone know some way to do something at least similar to what function pointers do without using them? Thanks for your help! :3
With the amount of detail you've given, I'm going to go with "no".

What kind of software? Library? Program? Language?
What do you mean it "doesn't support function pointers"?
What are you trying to do that needs function pointers? Function pointers have analogues in nearly all situations.
I'm using RobotC. It is like C++ but has some differences, it is used for programming robots.

I'm not really sure, function pointers are just not a thing in RobotC I guess. I read somewhere that it doesn't call functions in the traditional way?

I'm trying to program a user controlled menu kinda. The program has six different ways of doing something but it only needs to do one once we get on the field.

Basic example of how I would have done it with function pointers:


if (PosRampYes == 0)
{
FunctionPointerOne = FunctionOneBox();
FunctionPointerTwo = FunctionTwoBox();
FunctionPointerThree = FunctionThreeBox();
}
else
{
FunctionPointerOne = FunctionOneRamp();
FunctionPointerTwo = FunctionTwoRamp();
FunctionPointerThree = FunctionThreeRamp();
}

if (nNxtButtonPressed == 0)
FunctionPointerGoalOne = FunctionPointerOne;

if (nNxtButtonPressed == 1)
FunctionPointerGoalOne = FunctionPointerTwo;

if (nNxtButtonPressed == 2)
FunctionPointerGoalOne = FunctionPointerThree;

During this menu the user basically narrows it down to the one function we will need out of the six and it is set to"FunctionPointerGoalOne" so it will be run later in the program.

The problem is that function pointers aren't a thing in RobotC so I'm not sure how else to do this.
In this case you do not really need function pointers as you can make same with careful use of conditions.

If you want pointer-like entitiesm there is several possibilities:
a) Store not the pointer to function, but its index or code, or something, you can use to call it later:
1
2
3
4
5
6
7
8
9
if(something)
    func = 1; //Or CALL_FOO enumeration
else
    funct = 2; //Or CALL_BAR
//...
switch(funct) {
case 1: foo(); break;
case2: bar(); break;
}
or even wrap it into functor
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
class caller
{
public:
    void operator()()
    {
        switch(call) {
            case FOO: foo(); break;
            case BAR: bar(); break;
        }
    }

    enum funct
    {
        FOO,
        BAR
    };
    funct call;
};
//...
caller baz;
baz.funct = baz::FOO;
baz();


2) Make use of dynamic dispatch:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
class caller
{
public:
    virtual void operator()() = 0;
    virtual ~caller(){}
};

class foo_caller: public caller
{
public:
    virtual void operator()()
    {
        foo();
    }
};

class bar_caller: public caller
{
public:
    virtual void operator()()
    {
        bar();
    }
};

caller* baz;
if (smth)
    baz = new foo_caller;
else
    baz = new bar_caller;
*baz();
It is better to wrap caller in another class which will hold it and give you proper value semantic and resourse management while preserving polymorfism.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
void no_function_pointers(int PosRampYes, int nNxtButtonPressed /*other parameters*/){
    if (PosRampYes){
        if (nNxtButtonPressed == 0)
            FunctionOneBox(/*other parameters*/);
        else if (nNxtButtonPressed == 1)
            FunctionTwoBox(/*other parameters*/);
        else if (nNxtButtonPressed == 3)
            FunctionThreeBox(/*other parameters*/);
    }else{
        if (nNxtButtonPressed == 0)
            FunctionOneRamp(/*other parameters*/);
        else if (nNxtButtonPressed == 1)
            FunctionTwoRamp(/*other parameters*/);
        else if (nNxtButtonPressed == 3)
            FunctionThreeRamp(/*other parameters*/);
    }
}

It can be further simplified:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
void no_function_pointers(int PosRampYes, int nNxtButtonPressed /*other parameters*/){
    int index = nNxtButtonPressed * 2 + PosRampYes;
    switch (index){
        case 0:
            FunctionOneRamp(/*other parameters*/);
            break;
        case 1:
            FunctionOneBox(/*other parameters*/);
            break;
        case 2:
            FunctionTwoRamp(/*other parameters*/);
            break;
        case 3:
            FunctionTwoBox(/*other parameters*/);
            break;
        case 4:
            FunctionThreeRamp(/*other parameters*/);
            break;
        case 5:
            FunctionThreeBox(/*other parameters*/);
            break;
    }
}
Topic archived. No new replies allowed.