假设我有以下功能
int watchVar(const char* var, const char* descriptor,
Color (*colorfunc)(const char* var) = yellowColorFunc)
与
Color yellowColorFunc(const void* var){
return Color::yellow();
}
我想重载 watchVar
以接受参数为 char
、int
、float
等的函数,但不想为每种类型创建默认颜色函数。
g++ 给出了这个错误:
xpcc::glcd::Color (*)(const char*)' has type 'xpcc::glcd::Color(const void*)
除了将 colorfunc 声明为空指针并强制调用者稍后自己转换参数之外,还有其他方法吗?
谢谢
最佳答案
函数指针被声明为 const char *
但 yellowColorFunc 被声明为 const void *
关于c++函数采用具有默认函数的函数指针,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18091135/