Reputation: 423
assume I have the following function
int watchVar(const char* var, const char* descriptor,
Color (*colorfunc)(const char* var) = yellowColorFunc)
with
Color yellowColorFunc(const void* var){
return Color::yellow();
}
I want to overload watchVar
to accept functions whos parameters are char
, int
, float
, etc, but do not want to create a default color function for each type.
g++ gives this error:
xpcc::glcd::Color (*)(const char*)' has type 'xpcc::glcd::Color(const void*)
Is there another way besides declaring colorfunc to take a void pointer and forcing the caller to cast the argument later himself?
Thanks
Upvotes: 1
Views: 212
Reputation: 18750
Your problem is that your declaring a function pointer taking a const char *
but yellowColorFun
takes a const void *
. If c++11 is available you can use std::function
like so:
auto colorFunc = std::function<int(const char *,const char *,std::function<Color(const char*)>)>();
You said in a comment that you wanted to use the function for int,float
and others, what your should do in that situation is use a templated function, you really don't wanna use void*
's in c++ very often.
Upvotes: 2
Reputation: 5459
the function pointer is declared const char *
but the yellowColorFunc is declared const void *
Upvotes: 4