void* pointing to function pointer

Morning fellows,

I'm stuck in a "passing functions as a function's parameters" issue. I'm trying to make a function that, passing to it other functions, it joints them together in an array of functions.

So far, my problem is that, in order to make my function as "portable" as possible, I've declared it with void pointers as parameters:

1
2
3
4
void functionArrayCreator (void* A = 0,  void* B = 0,  void* C = 0,  void* D = 0,  void* E = 0,
void* F = 0, void* G = 0, void* H = 0, void* I = 0, void* J = 0); 
// It will accept a maximum of 10 functions (more than enough)
// The unused pointers will remain null 


But when I try to pass a function as a parameter:

1
2
void firstFunction (double*, int);
functionArrayCreator (*firstFunction);


My compiler screams out:
error: invalid conversion from ‘void (*)(double*, int)’ to ‘void*’ [-fpermissive]


So, my question is, am I doing something wrong, or void* can't point to a function?

Thanks!
Last edited on
Thinking it twice, it wouldn't be too tricky to do it with templates....
You are trying to use very compound C++ constructions.

It is better if the function will have only one parameter a pointer to a function pointer. In this case you will be able to pass an array of function pointers to your function.


For example

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
#include <iostream>

void f0( double *, int ) { std::cout << "f0()\n"; }
void f1( double *, int ) { std::cout << "f1()\n"; }
void f2( double *, int ) { std::cout << "f2()\n"; }
void f3( double *, int ) { std::cout << "f3()\n"; }
void f4( double *, int ) { std::cout << "f4()\n"; }
void f5( double *, int ) { std::cout << "f5()\n"; }
void f6( double *, int ) { std::cout << "f6()\n"; }

void fn( void ( **fp )( double *, int ) )
{
	int i = 0;
	while ( fp[i] ) fp[i++]( 0, 0 );
}

int main()
{
   const int N = 10;
   void ( *fp[N] )( double *, int ) = { f0, f1, f2, f3, f4, f5, f6 }; // all other after f6 are zeroes

   fn( fp );

   return ( 0 );
}
Last edited on
Shit, I'm stuck with templates either:

1
2
3
4
5
6
7
template <class A=bool, class B=bool, class C=bool, class D=bool, class E=bool,
 class F=bool, class G=bool, class H=bool, class I=bool, class J=bool> 
   void ** functionArrayCreator (A* a= 0, B* b = 0, C* c= 0, D* d= 0, E* e= 0,F* f= 0,G* g= 0, H* h= 0,
    I* i= 0, J* j= 0){
//...
}


error: default template arguments may not be used in function templates without -std=c++0x or -std=gnu++0x


Seems to be "forbidden" the use of default template arguments with function-templates?
----------------------------------
edit:
O no! this output means I need a c++11 capable IDE, doesn't it? Didn't think this moment would ever arrive... :(
Last edited on
The error says that you shall switch on supporting of the new C++ standard.
Last edited on
You are trying to use very compound C++ constructions.

It is better if the function will have only one parameter a pointer to a function pointer. In this case you will be able to pass an array of function pointers to your function.


Thoughtful idea. Though, my goal is to be able to pass different functions with different parameters, in order to use this function no matter what function comes in the future...

Anyway, I'll try with my template with a more modern IDE...
Well, finally I made it work with code::blocks and default template parameters to function-parameter's support:

1
2
3
4
5
6
7
8
9
10
11
12
template <class A=bool, class B=bool, class C=bool, class D=bool, class E=bool,
 class F=bool, class G=bool, class H=bool, class I=bool, class J=bool> 
   void ** functionArrayCreator (A a= 0 , B b = 0 , C c= 0 , D d= 0 , E e= 0 ,F f= 0 ,G g= 0 , H h= 0,
    I i= 0 , J j= 0){
//...
}

void firstFunction (double*, int);
void secondFunction (string, int, int);

functionArrayCreator <void (*) (double*, int), void (*) (string, int, int)> (*firstFunction, *secondFunction);


Though, I still want to keep using KDevelop. I've seen they claim that in their last version it supports some basic c++0x, does anybody know if this feature in particular is enabled? How should I enable c++0x features in KDevelop (I assume that, as code::blocks, they aren't enabled by default)

thanks!
Last edited on
Topic archived. No new replies allowed.