1

I want to create a function prototype in C++ so that there is a void * argument that can take pointers of any type. I know that this is possible in C. Is it possible in C++?

[EDIT] Here is a simplified version of the code that I am trying to get to work:

#include <stdio.h>

void func(void (f)(const void *))
{
    int i = 3;
    (*f)(&i);
}

void func_i(const int *i)
{
    printf("i=%p\n",i);
}

void func_f(const float *f)
{
    printf("f=%p\n",f);
}

void bar()
{
    func(func_i);
}

And here is the compiler output:

$ g++ -c -Wall x.cpp
x.cpp: In function ‘void bar()’:
x.cpp:21: error: invalid conversion from ‘void (*)(const int*)’ to ‘void (*)(const void*)’
x.cpp:21: error:   initializing argument 1 of ‘void func(void (*)(const void*))’
$ %
vy32
  • 28,461
  • 37
  • 122
  • 246
  • 1
    What do you plan on doing with those pointers? – Mat Sep 25 '11 at 17:08
  • C++ is (nearly) a superset of C, so if your goal is just to do the same thing in C++ that you'd have done in C, then you can do that. But there's usually a better way, if you ask the right question. What are you actually trying t odo? – jalf Sep 25 '11 at 17:13
  • Hm. For some reason this is working in my small test case, but not in my big program. Let me work on the test case... – vy32 Sep 25 '11 at 17:16
  • What you have shown in the code certainly cannot be done in C++.Casting between void and any other must always be explicitly done. You would be better of with the template solution provided by Benoit – Arunmu Sep 25 '11 at 17:38
  • http://stackoverflow.com/questions/559581/casting-a-function-pointer-to-another-type .. this might interest you :) – Arunmu Sep 25 '11 at 17:41
  • Thanks. Unfortunately I can't use templates. I have an array of structures of function pointers. – vy32 Sep 25 '11 at 21:56
  • @ArunMu, if you are sure that this cannot be done, then please make that an answer and I will accept it. – vy32 Sep 25 '11 at 21:57

3 Answers3

4

You may use void*, just as with C, but you'll need to cast your argument when calling it. I suggest you use a template function

template<typename T>
void doSomething(T* t) {...}
Benoît
  • 16,798
  • 8
  • 46
  • 66
  • Unfortunately, this doesn't work in my application. I need to put all of the functions into an array. – vy32 Sep 25 '11 at 21:57
  • @vy32: Can you please explain in more detail what exactly you need to do ? Using void * may not be needed at all !! you may be able to do what you want with boost bind and boost function – Arunmu Sep 26 '11 at 03:51
1

How about:

void func(void *);

exactly like in C? : P

NiematojakTomasz
  • 2,433
  • 20
  • 23
1

Yes.

int i = 345;
void * ptr = &i;
int k = *static_cast&lt int* &gt(ptr);

UPDATE ::
What you have shown in the code certainly cannot be done in C++.
Casting between void and any other must always be explicitly done.

Check these SO link for more details on what the C -standard has to say:
1) http://stackoverflow.com/questions/188839/function-pointer-cast-to-different-signature
2) http://stackoverflow.com/questions/559581/casting-a-function-pointer-to-another-type
Arunmu
  • 6,837
  • 1
  • 24
  • 46