I need to pass something like a pointer that takes anything as a function parameter. You know, something without any predefined type or a type that can take anything like this:
void MyFunc( *pointer );
And then use it like:
char * x = "YAY!";
MyFunc(x);
int y = 10;
MyFunc(&y);
MyObj *b = new MyObj();
MyFunc(b);
And I don’t want to use templates because I am mostly using C in my project.
Is there anything that can be used here except a function macro?
In C++, Boost.Any will let you do this in a type-safe way:
In C, you would use a void pointer:
You seem adverse to it but I’ll recommend it anyway: if you’re using C++, embrace it. Don’t be afraid of templates. Things like Boost.Any and void pointers have a place in C++, but it is very small.
Update:
If you need multi-target signals, Boost.Signals already provides a full and tested signals/slots implementation. You can use Boost.Bind (or
std::bind, if you’ve got a C++0x compiler) to connect member functions:If you only want a simple callback, Boost.Function (or
std::functionif you’ve got a C++0x compiler) will work well: