Delegation function.

Hello,

I have a series of basic functions; at certain points in my program, a parameter is checked and a corresponding function is called. I do this through a delegation function:
1
2
3
4
5
6
int calculate(X) {
    switch(someParam) {
    case 1: return function1(X);
    case 2: return function2(X);
//...etc    
}

This has always worked well, but now I find myself in a slightly different situation: rather than single calls, I need to update an entire array of several million elements, with the same function. Thus, by checking the parameter only once, I know which function to call in the entire process.

Obviously, rechecking through the delegation function is useless, but I can't find an elegant way around it. Anything I can think of either involves doubling up a lot of code, or redesigning my class layout.

Is there a way to "remember" a function name? I'm thinking something in line of binding a reference to a function, but I'm not sure that exists...

Any ideas?
Is there a way to bind a reference to a function? Why yes, it's called a function pointer: http://www.newty.de/fpt/index.html
If all the functions have the same signature, you could have calculate return a function pointer rather than actually call the function.

My function pointer syntax is fuzzy, but I think this is right:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
typedef int (*FuncPtr)(int);

FuncPtr GetCalcFunction()
{
  switch(someParam){
  case 1:  return &function1;
  case 2:  return &function2;
  //...
}


//...
FuncPtr func = GetCalcFunction( whatever );

for( /*...whatever...*/ )
{
  int result = (*func)(param);
}
You could have a function returning a function pointer.

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
32
33
34
int function1(int x)
{
	return 1;
}

int function2(int x)
{
	return 2;
}

typedef int (*Func)(int);

Func functionX(int someParam) 
{
	switch(someParam) 
	{
	case 1: return function1;
	case 2: return function2;
	}
}

int calculate(int x, int someParam)
{
	return functionX(someParam)(x);
}

void updateArray(int* array, int size, int someParam)
{
	Func f = functionX(someParam);
	for (int i = 0; i < size; ++i)
	{
		array[i] = f(i);
	}
}

Last edited on
Thanks for the reply! Looks like exactly what I need!

Haven't gotten it to work yet, though. I think it might have something to do with the fact that it's a member function [so is the function that returns it], or that it's "const". The functions I want to return look like this one:
inline bool Solution::CGet(const int i, const int j) const { .. }
Yes if it's a member function that's different because it has to be called with an object. The syntax for that is a bit uglier.

Here's a page I often go to to refresh myself on the syntax for these:

http://www.newty.de/fpt/fpt.html#assign
Wasn't so bad, apparently. Thanks!

(For future readers, this is how it looks now:
1
2
3
4
5
6
7
8
// Typedef somewhere early on
typedef bool (Solution::*CFunc)(const NODE, const NODE) const;
// Delegation function
inline CFunc Solution::CMove(void) const {
    switch(param) {
    case 0:   return &Solution::CRelocate;
// etc.
}

Big thanks to both of you!
Last edited on
Woops, premature jubilation.

Having troubles using it. I'm trying this:
1
2
3
4
5
// Get correct V and C funcs.
CFunc C = CMove();
VFunc V = VMove();
// Using them
(this->*C)(i, j);


I'm getting this error:
>SMD.obj : error LNK2019: unresolved external symbol "public: int (__thiscall Solution::*__thiscall Solution::VMove(void)const )(unsigned int,unsigned int)const " (?VMove@Solution@@QBEP81@BEHII@ZXZ) referenced in function "public: void __thiscall Solution::SInit(void)" (?SInit@Solution@@QAEXXZ)

It's a bit sad, but I have no idea what the problem is. All functions involved are declared in the same .h file (inside the Solution class); the definitions are spread across different files, but they definitely exist.
Topic archived. No new replies allowed.